Java org.openqa.selenium.ElementNotVisibleException:元素当前不可见,因此可能无法与之交互
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25917132/
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
org.openqa.selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with
提问by Shiva krishna Chippa
I am trying to execute below Selenium Web driver script, But I am getting org.openqa.selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with
error several time(not all the times). sometimes in loops first iteration and sometimes in 2 iteration and sometimes without starting loop. It printing all the available items count correctly but whey trying to click on items, it showing Element is not currently visible...
我正在尝试在 Selenium Web 驱动程序脚本下执行,但我org.openqa.selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with
多次(并非总是)遇到错误。有时在循环第一次迭代中,有时在 2 次迭代中,有时没有开始循环。它打印所有可用的项目计数正确,但乳清试图点击项目,它显示Element is not currently visible...
public void pickitems() throws Exception
{
Webdriver driver = new firefoxdriver();
driver.get("http://www.bigbasket.com");
driver.manage().window().maximize();
//Selecting Location
List<WebElement> list = driver.findElement(By.id("ftv-city-popup")).findElements(By.tagName("button"));
int location = r.nextInt(list.size());
list.get(location).click();
//Selection random Category from left panel through list
Thread.sleep(30000);
List<WebElement> xyz = driver.findElement(By.id("uiv2-main-menu")).findElements(By.className("top-category"));
System.out.println(xyz.size());
Random r = new Random();
int category = r.nextInt(xyz.size());
xyz.get(category).click();
for (int i = 0; i < 3; i++) {
Thread.sleep(30000);
List<WebElement> availableItems = driver.findElements(By.cssSelector("a.uiv2-add-button.a2c"));
System.out.println(availableItems.size());
if (availableItems.size() > 0)
{
int selectItem = r.nextInt(availableItems.size());
availableItems.get(selectItem).click();
}
else
{
Thread.sleep(30000);
List<WebElement> availableItems2 = driver.findElements(By.cssSelector("a.uiv2-add-button.a2c"));
if (availableItems2.size() == 0) {
System.out.println("No more items are available. Sorry for the inconvenience");
}
else {
Assert.fail("Unable to select items. May be page loading issue");
}
}
}
}
}
采纳答案by Akshay
Finally this worked for me. Element is not currently visible and so may not be interacted with.
最后这对我有用。元素当前不可见,因此可能无法与之交互。
Initially it was like test was successful only 2 of 5 times. Was not sure how it was working sometimes and others not.
最初它就像测试只成功了 5 次中的 2 次。有时不确定它是如何工作的,而其他人则不是。
Worked by reducing the security settings in IE. Enable all activeX controls. Enable scripts and IFRAMES also. Some of these will warn to put computer at risk, but it was the only solution I had. Introduce Explicit wait by using presenceOfElementLocated instead of visibilityOfElementLocated at every point where page load takes time.
通过减少 IE 中的安全设置来工作。启用所有 ActiveX 控件。还启用脚本和 IFRAMES。其中一些会警告使计算机处于危险之中,但这是我唯一的解决方案。通过在页面加载需要时间的每个点使用presenceOfElementLocated 而不是visibilityOfElementLocated 来引入显式等待。
WebDriverWait wait = new WebDriverWait(driver,30);
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[@id='campaignListTable']"))); /*examining the xpath for a search
box*/
driver.findElement(By.xpath("//*[@id='campaignListTable']")).sendKeys("TEXT"); /*enter text in search
box*/
回答by Saifur
Not sure what your requirement is. But, couple of things to keep in mind.
不确定你的要求是什么。但是,有几件事要记住。
- Selenium may find the element that meets same criteria but they are hidden
- Even if element is not hidden it may not be in a ready state to accept any interaction.
- Selenium 可能会找到满足相同条件但它们被隐藏的元素
- 即使元素没有隐藏,它也可能不会处于接受任何交互的就绪状态。
if you know for sure the element is not hidden then you can use the following wait for the element to be visible
如果您确定元素未隐藏,则可以使用以下命令等待元素可见
new WebDriverWait(Driver, TimeSpan.FromSeconds(10)).Until(ExpectedConditions.ElementIsVisible("your selector");
回答by Rishi Khanna
I guess you are trying to click on hyperlinks, if you are getting 'ElementNotVisibleException' this means that elements might be hidden. Does it take long time for the elements with locator 'a.uiv2-add-button.a2c' to render on the UI after you select a Category from the left panel? If yes than interaction wiith non visible elements will always throw 'ElementNotVisibleException'
我猜您正在尝试单击超链接,如果您收到“ElementNotVisibleException”,这意味着元素可能被隐藏。从左侧面板中选择一个类别后,带有定位器 'a.uiv2-add-button.a2c' 的元素是否需要很长时间才能在 UI 上呈现?如果是,则与不可见元素的交互将始终抛出“ElementNotVisibleException”
回答by Dharshan Sithamparam
Just put the thread to sleep for some mil seconds
只需让线程休眠几百万秒
Thread.sleep(5000);
WebElement zoneName=driver.findElement(By.xpath("//*[@id=\"zoneName\"]"));
zoneName.sendKeys("kandy");
回答by Bheemashankar Pattar
For some browsers it happens that once mouse hover action is performed, but the menu list disappear quickly before Selenium identify the next sub menu item. In that case it is better to use perform()
action on the main menu to hold the menu list till the time Selenium identify the sub menu item and click on it.
对于某些浏览器,一旦执行鼠标悬停操作,菜单列表就会在 Selenium 识别下一个子菜单项之前迅速消失。在这种情况下,最好perform()
在主菜单上使用操作来保持菜单列表,直到 Selenium 识别子菜单项并单击它。
Here
这里
WebElement xWL = driver.findElement(By.xpath("x path text"));
Actions xAct = new Actions(driver);
Instead of this:
取而代之的是:
xAct.moveToElement(xWL).build().perform();
Below code will resolve the "element not visible" issue
下面的代码将解决“元素不可见”问题
xAct.moveToElement(xWL);
xAct.click();
xAct.perform();
回答by Arun Siddharth
I had a similar issue and the reason was, there were more hidden elements along with the actual element that i tried to find. So click was actually trying to interact with hidden element and throwed the exception.
我有一个类似的问题,原因是,除了我试图找到的实际元素之外,还有更多隐藏元素。所以 click 实际上是试图与隐藏元素进行交互并抛出异常。
Solution- I fine-tuned the element's xpath to make it unique which resulted the interaction to the actual element.
解决方案 - 我微调了元素的 xpath 以使其独一无二,从而导致与实际元素的交互。
回答by Vinee-the-Pooh
To handle it , you can use explicit wait function in selenium to locate element. Most of the time it works.
要处理它,您可以使用 selenium 中的显式等待函数来定位元素。大多数时候它都有效。