javascript Selenium Webdriver:元素当前不可见

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20390459/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 18:23:28  来源:igfitidea点击:

Selenium Webdriver: Element is not currently visible

javascriptjqueryhtmlseleniumselenium-webdriver

提问by testing

I used the same type of code to select the drop down there the code is working fine but in this case i tried to click on the button. am getting the error as Element is not currently visible and so may not be interacted with Command duration or timeout: 31 milliseconds

我使用相同类型的代码来选择代码工作正常的下拉列表,但在这种情况下,我尝试单击按钮。我收到错误,因为元素当前不可见,因此可能无法与命令持续时间或超时交互:31 毫秒

JavascriptExecutor executor3 = (JavascriptExecutor)driver;
executor3.executeScript("document.getElementById('iskpiFilterAction').style.display='block';");
driver.findElement(By.id("iskpiFilterAction")).click();
Thread.sleep(6000); 

The Problem is the type is hidden and the html tags as follows:

问题是类型被隐藏,html标签如下:

<input id="iskpiFilterAction" type="hidden" value="1" name="isKpiFilterAction">

Can any one please check the code and give me solution or sample code.

任何人都可以检查代码并给我解决方案或示例代码。

回答by aaronfay

As epascarello mentioned in the comment, #iskpiFilterActionis nota button, it's a hidden <input>element. Therefore, you can't click()it here:

作为epascarello在评论中提到,#iskpiFilterAction不是一个按钮,它是一个隐藏的<input>元素。因此,你不能click()在这里:

driver.findElement(By.id("iskpiFilterAction")).click(); // this won't work

回答by djangofan

Also, what people usually do with a element like that, which is either visible or not is use ExpectedCondition.visibilityOfElement()like so:

此外,人们通常对这样的元素(可见或不可见)所做的事情是这样使用的ExpectedCondition.visibilityOfElement()

WebElement foo2 = wait.until(ExpectedConditions
      .visibilityOfElementLocated(By.id("iskpiFilterAction")));

Whereas, the regular way of getting a element might be:

然而,获取元素的常规方法可能是:

WebElement foo2 = wait.until(ExpectedConditions
      .presenceOfElementLocated(By.id("iskpiFilterAction")));

回答by fil mihaylov

You can try to use Selenuim Actions class to simulate user interactions which will make the button visible to the user--

您可以尝试使用 Selenuim Actions 类来模拟用户交互,这将使按钮对用户可见--

for example:

例如:

WebElement menu = driver.findElement(By.xpath("));

WebElement menu = driver.findElement(By.xpath("));

    Actions build = new Actions(driver);
    build.moveToElement(menu).build().perform();//Hovers the mouse over the first element which will trigger the event
    WebElement m2m= driver.findElement(By.xpath(""));// finds the previouslly hidden element
    m2m.click();