2014-09-18 134 views
4

交互我试图执行下面的Selenium Web驱动程序脚本,但我得到org.openqa.selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with错误几次(并非所有的时间)。有时在循环中第一次迭代,有时在2次迭代中,有时没有开始循环。它打印所有可用的项目算错,但乳清试图点击项目,它显示Element is not currently visible...org.openqa.selenium.ElementNotVisibleException:元素当前不可见,因此可能不会与

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"); 
       } 


      } 

     } 

    } 

} 

回答

6

最后这对我有效。 元素当前不可见,因此可能不会与之交互。

最初它就像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*/ 
6

不知道你的要求是什么。但是,请牢记几件事情。

  1. 硒可以找到符合同一标准的元素,但它们是隐藏
  2. 即使元素不是隐藏它可能不处于就绪状态接受任何干预。

,如果你肯定知道的元素没有被隐藏,那么你可以使用下面的等待元素可见

new WebDriverWait(Driver, TimeSpan.FromSeconds(10)).Until(ExpectedConditions.ElementIsVisible("your selector"); 
+0

感谢。这解决了我的问题。 – 2015-02-26 05:14:05

+0

@JakeW。很高兴帮助你 – Saifur 2015-02-26 13:29:47

+0

你能帮我把这个命令转换成JS吗?我有同样的问题,但我在Node.JS的量角器中使用。谢谢@Saifur – 2016-06-09 11:46:04

0

我猜你是想通过点击超链接,如果你正在'ElementNotVisibleException'这意味着元素可能被隐藏。从左侧面板选择类别后,在UI上呈现定位器“a.uiv2-add-button.a2c”的元素是否需要很长时间?如果是,那么交互与不可见元素总是会抛出'ElementNotVisibleException'

-1

只要把线程休眠一段密尔秒

Thread.sleep(5000); 
WebElement zoneName=driver.findElement(By.xpath("//*[@id=\"zoneName\"]")); 
zoneName.sendKeys("kandy"); 
0

对于某些浏览器碰巧一旦执行鼠标悬停动作,但在菜单列表很快消失之前硒确定下一子菜单项目。在这种情况下,最好在主菜单上使用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(); 
相关问题