GVKun编程网logo

Selenium Webdriver:元素不可见异常(selenium元素不可点击)

6

这篇文章主要围绕SeleniumWebdriver:元素不可见异常和selenium元素不可点击展开,旨在为您提供一份详细的参考资料。我们将全面介绍SeleniumWebdriver:元素不可见异常的

这篇文章主要围绕Selenium Webdriver:元素不可见异常selenium元素不可点击展开,旨在为您提供一份详细的参考资料。我们将全面介绍Selenium Webdriver:元素不可见异常的优缺点,解答selenium元素不可点击的相关问题,同时也会为您带来Java webdriver:元素不可见异常、Java 如何强制Selenium WebDriver单击当前不可见的元素?、JavaSelenium Webdriver:修改navigator.webdriver标志以防止selenium检测、org.openqa.selenium.ElementNotVisibleException:通过SeleniumWebDriver和Java单击复选框时,元素当前不可见的实用方法。

本文目录一览:

Selenium Webdriver:元素不可见异常(selenium元素不可点击)

Selenium Webdriver:元素不可见异常(selenium元素不可点击)

这是我的代码,可单击该网站上的简单登录按钮

import java.util.concurrent.TimeUnit;import org.openqa.selenium.By;    import org.openqa.selenium.WebDriver;    import org.openqa.selenium.firefox.FirefoxDriver;public class Reports {    public static void main(String[] args) {        WebDriver driver = new FirefoxDriver();        driver.get("https://platform.drawbrid.ge");        driver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS);        driver.findElement(By.xpath(".//*[@id=''_loginButton'']")).click();    }}

我收到以下错误:

线程“主”中的异常org.openqa.selenium.ElementNotVisibleException:元素当前不可见,因此可能无法与命令持续时间或超时进行交互:2.05秒

答案1

小编典典

在此页面上,您有两个具有给定xpath的按钮,第一个不可见,这就是为什么您收到ElementNotVisibleException的原因

一个在下 <div>

第二(您需要的那个)在 <div>

因此,将xpath更改为如下所示,它将解决您的问题:

By.xpath("//div[@page'']//div[@id=''_loginButton'']")

Java webdriver:元素不可见异常

Java webdriver:元素不可见异常

我有以下问题.我有一个隐藏的下拉列表,所以当我进行选择并运行测试时,我得到以下错误:
org.openqa.selenium.ElementNotVisibleException: element not visible: Element is not currently visible and may not be manipulated
  (Session info: chrome=30.0.1599.101)

这是我的选择:

Select s = new Select(dropDown);
s.selectByVisibleText("CHARGEBACK");

是否有一个漫步来操纵隐藏的元素?我在其中一篇帖子中找到了以下代码:

JavascriptExecutor jse = (JavascriptExecutor) driver;
 jse.executeScript("arguments[0].scrollIntoView(true);",element);

这是HTML代码:

<div>
<select id="formlevel:levels_input" name="formlevel:levels_input">
<option value="541fac58-5ea8-44ef-9664-e7e48b6c6a3c">Seleccione un Registro</option>
<option value="dafc799c-4d5e-4b02-a882-74cb6ad98902">Security</option>
<option value="e5416086-2036-4cd0-b23e-865747aa3f53">CALL CENTER</option>
<option value="7ea4b4ea-4f06-4d27-9541-1b0cf3f2aa22">CHARGEBACK</option>
<option value="0f915120-7b8f-4a33-b063-5d20a834b655">PREVENÇÃO A FRAUDE</option>
<option value="a8ef13e8-f4a5-43b8-a668-b769f6988565">ANALISE DE CREDITO</option>
<option value="83b65a26-d4cd-43d3-b3fa-2f7894ca454a">SUPORTE A CONTA</option>
<option value="163d0db9-590c-47a7-a271-218b2d27d8d9">REGULARIZAÇÃO FINANCEIRA</option>


它在这种情况下不起作用.任何帮助,将不胜感激.

解决方法

由于WebDriver尝试模拟真实用户,因此无法与不可见/隐藏的元素进行交互.为了解决您的问题,我认为您需要首先单击div,这将使下拉显示,并从下拉列表中选择选项.我建议使用这种方法而不是纯Javascript方式,因为它会模拟真实用户.跟着一枪,
webdriverwait wait = new webdriverwait(driver,300);
WebElement triggerDropDown = driver.findElement(By
                .className("ui-helper-hidden"));
triggerDropDown.click();
WebElement selectElement = wait.until(ExpectedConditions
                  .visibilityOfElementLocated(By.id("formlevel:levels_input")));
Select select = new Select(selectElement);
select.selectByVisibleText("Security");

编辑更新了triggerDropDown的类名

Java 如何强制Selenium WebDriver单击当前不可见的元素?

Java 如何强制Selenium WebDriver单击当前不可见的元素?

我在FirefoxDriver中使用Selenium 2 Java API。当我填写表单时,根据表单输入,将复选框添加到页面。

我想使用Selenium模拟对这些复选框的单击。该元素在常规浏览器中可见且可用,但是selenium断言该元素不可见。

"Element is not currently visible and so may not be interacted with"

我可以强迫selenium忽略元素的不可见状态吗?如何强制Selenium与不可见元素进行交互?

答案1

小编典典

Selenium根据以下条件确定元素是否可见(使用DOM检查器确定哪些CSS适用于你的元素,请确保你查看计算的样式):

  • visibility != hidden
  • display != none (is also checked against every parent element)
  • opacity != 0 (this is not checked for clicking an element)
  • height and width are both > 0
  • for an input, the attribute type != hidden

你的元素符合这些条件之一。如果你没有能力更改元素的样式,请按照以下方法用javascript强制执行此操作(由于你使用了Selenium2 API,因此假设使用WebDriver):

((JavascriptExecutor)driver).executeScript("arguments[0].checked = true;", inputElement);

但这不会触发javascript事件,如果你依赖于该输入的change事件,那么你也必须触发它(有多种方法,最容易使用该页面上加载的任何javascript库)。

JavaSelenium Webdriver:修改navigator.webdriver标志以防止selenium检测

JavaSelenium Webdriver:修改navigator.webdriver标志以防止selenium检测

如何解决JavaSelenium Webdriver:修改navigator.webdriver标志以防止selenium检测?

从当前的实现开始,一种理想的访问网页而不被检测到的方法是使用ChromeOptions()该类向以下参数添加几个参数:

排除enable-automation开关的集合 关掉 useAutomationExtension 通过以下实例ChromeOptions

Java示例:

System.setProperty("webdriver.chrome.driver", "C:\\Utility\\browserDrivers\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("excludeSwitches", Collections.singletonList("enable-automation"));
options.setExperimentalOption("useAutomationExtension", false);
WebDriver driver =  new ChromeDriver(options);
driver.get("https://www.google.com/");

Python范例

from selenium import webdriver

options = webdriver.ChromeOptions()
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option(''useAutomationExtension'', False)
driver = webdriver.Chrome(options=options, executable_path=r''C:\path\to\chromedriver.exe'')
driver.get("https://www.google.com/")

解决方法

我正在尝试使用selenium和铬在网站中自动化一个非常基本的任务,但是以某种方式网站会检测到铬是由selenium驱动的,并阻止每个请求。我怀疑该网站是否依赖像这样的公开DOM变量https://stackoverflow.com/a/41904453/648236来检测selenium驱动的浏览器。

我的问题是,有没有办法使navigator.webdriver标志为假?我愿意尝试修改后重新尝试编译selenium源,但是似乎无法在存储库中的任何地方找到NavigatorAutomationInformation源https://github.com/SeleniumHQ/selenium

任何帮助深表感谢

PS:我还从https://w3c.github.io/webdriver/#interface尝试了以下操作

Object.defineProperty(navigator,''webdriver'',{
    get: () => false,});

但是它仅在初始页面加载后更新属性。我认为网站会在执行脚本之前检测到变量。

org.openqa.selenium.ElementNotVisibleException:通过SeleniumWebDriver和Java单击复选框时,元素当前不可见

org.openqa.selenium.ElementNotVisibleException:通过SeleniumWebDriver和Java单击复选框时,元素当前不可见

我必须选中以下HTML代码段中包含的复选框。该复选框包含在输入标签中

<div formarrayname="entityTypes" fxlayout="" fxlayoutgap="10px">

                  <divfxlayout="row" fxlayoutgap="10px">

                    <app-checkbox formcontrolname="isSelected" _nghost-c26=""><div _ngcontent-c26="">



  <span _ngcontent-c26=""fxlayout="row" fxlayoutalign="start center">

    <!---->

    <input _ngcontent-c26="" type="checkbox" name="undefined"xpath="1">



      Funder

      <label _ngcontent-c26=""></label>

  </span>



  <!---->



  <!---->



</div>

</app-checkbox>

                  </div>

                </div>

我已经尝试过各种方法来识别并选择它,但是它从来都不可见。我已将复选框标签的文本打印到控制台,因此无法理解为什么复选框本身不可见。以下Java代码成功打印了标签,但未能单击复选框,并抛出错误元素不可见。

//print text of input box
WebElement labelFunder = driver.findElement(By.xpath("//div[@fxflex='50']//div[3]//div[1]//app-checkbox[1]//div[1]//span[1]//input[1]"));
String textFunderLabel2 = labelFunder.getAttribute("innerText").toString();
System.out.println(textFunderLabel);
labelFunder.click();

我尝试了不同的等待,但均未成功。

//select the funder checkbox
//driver.findElement(By.xpath("//div[@fxflex='50']//div[3]//div[1]//app-checkbox[1]//div[1]//span[1]//input[@type='checkbox']")).click();
//WebDriverWait wait = new WebDriverWait(driver,30);
//WebElement checkbox = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("/html[1]/body[1]/app-root[1]/main[1]/section[1]/div[2]/app-company-detail[1]/div[2]/form[1]/md-tab-group[1]/div[1]/md-tab-body[1]/div[1]/div[1]/div[2]/div[1]/div[2]/div[3]/div[1]/app-checkbox[1]/div[1]/span[1]/input[1]")));
//checkbox.click();

任何人都可以在这里向我指出正确的方向

谢谢。

今天关于Selenium Webdriver:元素不可见异常selenium元素不可点击的介绍到此结束,谢谢您的阅读,有关Java webdriver:元素不可见异常、Java 如何强制Selenium WebDriver单击当前不可见的元素?、JavaSelenium Webdriver:修改navigator.webdriver标志以防止selenium检测、org.openqa.selenium.ElementNotVisibleException:通过SeleniumWebDriver和Java单击复选框时,元素当前不可见等更多相关知识的信息可以在本站进行查询。

本文标签: