java - 元素不可见 : Element is not currently visible and may not be manipulated

标签 java selenium selenium-webdriver

我正在尝试从下拉列表中选择一个元素。当我使用 getAttribute() 方法时,我已检索到 12 个元素:

    Select select = new Select(driver.findElement(By.xpath("//select[@id='dataset_downloadDataset_select']")));
    List<WebElement> options = select.getOptions();
    System.out.println(options.size());
    for (int i=1; i<=11; i++){
    System.out.println(options.get(i).getAttribute("value"));

检索 dropdowm 的 12 个元素后,我想选择其中一个。为此,我尝试了 Actions/Javascriptexecutor 但出现元素不可见异常。Action 方法使用的代码是:

    WebElement mnuElement;
    WebElement submnuElement;

    mnuElement = driver.findElement(By.xpath("//input[starts-with(@data-activates,'sele')][@value='XXXXXXXXXXX']"));
    submnuElement = driver.findElement(By.xpath("//*[@id='dataset_downloadDataset_select']/option[4]"));
    Actions builder = new Actions(driver);
    builder.moveToElement(mnuElement).perform();
    Thread.sleep(5000);
    driver.findElement(By.xpath("//*[@id='dataset_downloadDataset_select']/option[4]")).click();

谁能帮我解决这个问题。

最佳答案

这取决于您使用的浏览器。 Chrome 可以点击选项而不扩展,而 Firefox 则无法处理。使用 select 进行选择。您使用的选项需要单击列表然后进行选择,但这样做更简单

Firefox(可能还有每个浏览器)的代码

WebDriver driver;//then choosing browser
element=driver.findElement(By.xpath("//select[@id='dataset_downloadDataset_select']"));//or whatever
Select select=new Select(element);

//To select what you want, this is selecting, nothing more

select.selectByValue("Mainframe File 1");//or other

关于java - 元素不可见 : Element is not currently visible and may not be manipulated,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41315640/

相关文章:

java - Firefox浏览器49.0版本是否不支持selenium Standalone server 2.50?

selenium - 页面工厂中ElementLocatorFactory和FieldDecorator的作用是什么?

python - 如果我的 chromedriver 与 chrome 版本不兼容,我该如何修复它?

python - 使用py2exe将python脚本编译为exe,出现错误

selenium - 如何根据同级输入的@id选择跨度

java - 在 DialogFragment 中使用 getIntent() 和 getPackageInfo

java - 如何使用 Selenium WebDriver 在 Chrome 中激活 AdBlocker?

java - 使用 Google/Facebook 帐户将连接添加到 JavaEE 应用程序

java - 如何在 Selenium Web Driver 中使用 SSL 证书?

java.lang.NoSuchMethodError : org/apache/http/conn/scheme/Scheme. <init>(Ljava/lang/String;ILorg/apache/http/conn/scheme/SchemeSocketFactory;)V