Java webdriver:元素不可见异常
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19637507/
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
Java webdriver: Element not visible exception
提问by elcharrua
I'm having the following problem. I have a dropdown that is hidden so when I make the Select and run the test i get the following error:
我有以下问题。我有一个隐藏的下拉列表,因此当我进行选择并运行测试时,出现以下错误:
org.openqa.selenium.ElementNotVisibleException: element not visible: Element is not currently visible and may not be manipulated
(Session info: chrome=30.0.1599.101)
This is my select:
这是我的选择:
Select s = new Select(dropDown);
s.selectByVisibleText("CHARGEBACK");
Is there a walk around it to manipulate hidden elements?. I found the following code in one of the posts:
有没有绕着它走来操纵隐藏元素?我在其中一篇文章中找到了以下代码:
JavascriptExecutor jse = (JavascriptExecutor) driver;
jse.executeScript("arguments[0].scrollIntoView(true);", element);
This is the html code:
这是html代码:
<div class="ui-helper-hidden">
<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>
And it doesn't work in this case. Any help would be appreciated.
在这种情况下它不起作用。任何帮助,将不胜感激。
采纳答案by nilesh
Since WebDriver
tries to simulate real users, it cannot interact with elements which are invisible/hidden. To solve your issue, I think you would need to click on div
first which will make the drop down visible and select option from the dropdown. I would recommend such an approach as opposed to pure Javascript way since it would simulate a real user. Give following a shot,
由于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");
Editupdated the class name of triggerDropDown
Edit更新了 triggerDropDown 的类名
回答by jmccure
Haven't tested this, but does the following work?
还没有测试过这个,但是下面的工作有效吗?
s.selectByValue("7ea4b4ea-4f06-4d27-9541-1b0cf3f2aa22");
s.selectByValue("7ea4b4ea-4f06-4d27-9541-1b0cf3f2aa22");
回答by alexey.chumagin
I absolutely agree to sircapsalot. You should hold application business logic and "do like an user". And use this hack for workarounds only.
我绝对同意sircapsalot。您应该掌握应用程序业务逻辑并“像用户一样做事”。并且仅将此 hack 用于解决方法。
Answer:
回答:
Try this way
试试这个方法
document.getElementById('formLevel:levels_input').options[3].selected = "true"
document.getElementById('formLevel:levels_input').options[3].selected = "true"
回答by Mukesh Otwani
Hi There can be so many reason for this. I also faced this issue number of times and solved using different ways.
您好 这可能有很多原因。我也多次遇到这个问题并使用不同的方法解决。
1- Using WebdriverWait that is also know as explicit wait
1- 使用 WebdriverWait,也称为显式等待
2- Using unique xpath- using xpath ways.
2- 使用独特的 xpath- 使用 xpath 方式。
3- Get the size of element then click or perform any action on first one.
3- 获取元素的大小,然后单击或对第一个执行任何操作。
I documented all solution here How to Solve Element not visible Exception
我在这里记录了所有解决方案如何解决元素不可见异常
回答by Brick
In addition to the reasons and issues raised by the earlier answers, I encountered another cause worth mentioning. In my case, a JavaScript on the page had to run after clicking a link on the page in order for the elements that I wanted to access to become visible. That's ok as long as your driver has JavaScript enabled. In my case, I was running without JavaScript, so even though the link was "clicked" programmatically, the elements were not becoming visible. I was using HtmlUnitDriver
with the default settings. Ultimately I switched to ChromeDriver
. (You can enable JavaScript on HtmlUnitDriver
, but that - for other reasons - was not enough for me in my case.)
除了前面的回答提出的原因和问题之外,我还遇到了另一个值得一提的原因。就我而言,在单击页面上的链接后,页面上的 JavaScript 必须运行,以便我想要访问的元素变得可见。这没关系,只要你的驱动程序中启用JavaScript。就我而言,我在没有 JavaScript 的情况下运行,因此即使以编程方式“单击”链接,元素也不会变得可见。我使用HtmlUnitDriver
的是默认设置。最终我切换到ChromeDriver
. (您可以在 上启用 JavaScript HtmlUnitDriver
,但是 - 由于其他原因 - 就我而言,这对我来说还不够。)