我正在尝试在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");
}
}
}
}
}
答案 0 :(得分:9)
最后这对我有用。 元素当前不可见,因此可能无法与之交互。
最初它就像测试只成功了5次中的2次。不知道它有时是如何工作而其他人不是。
通过减少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*/
答案 1 :(得分:7)
不确定您的要求是什么。但是,要牢记几件事。
如果您确定元素未被隐藏,那么您可以使用以下等待元素可见
new WebDriverWait(Driver, TimeSpan.FromSeconds(10)).Until(ExpectedConditions.ElementIsVisible("your selector");
答案 2 :(得分:0)
我猜你是想点击超链接,如果你得到的是ElementNotVisibleException&#39;这意味着可能隐藏元素。带定位器的元素需要很长时间吗?a.uiv2-add-button.a2c&#39;从左侧面板中选择一个类别后在UI上呈现?如果是,那么与非可见元素的交互将始终抛出&#39; ElementNotVisibleException&#39;
答案 3 :(得分:0)
对于某些浏览器,一旦执行了鼠标悬停操作,就会发生这种情况,但是在Selenium识别下一个子菜单项之前,菜单列表会很快消失。在这种情况下,最好在主菜单上使用perform()
操作来保存菜单列表,直到Selenium识别子菜单项并单击它为止。
下面
WebElement xWL = driver.findElement(By.xpath("x path text"));
Actions xAct = new Actions(driver);
而不是:
xAct.moveToElement(xWL).build().perform();
下面的代码将解决“元素不可见”问题
xAct.moveToElement(xWL);
xAct.click();
xAct.perform();
答案 4 :(得分:0)
要处理此问题,可以在硒中使用显式等待函数来定位元素。在大多数情况下,它都有效。
答案 5 :(得分:-1)
让线程休眠几秒钟
Thread.sleep(5000);
WebElement zoneName=driver.findElement(By.xpath("//*[@id=\"zoneName\"]"));
zoneName.sendKeys("kandy");
答案 6 :(得分:-1)
我有一个类似的问题,原因是, 还有更多隐藏的元素以及我尝试找到的实际元素。 因此,点击实际上是试图与隐藏元素进行交互并引发异常。
解决方案-我对元素的xpath进行了微调,使其具有唯一性,从而导致与实际元素的交互。