一尘不染

org.openqa.selenium.ElementNotVisibleException:元素当前不可见,因此可能无法与之交互

selenium

我正在尝试在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");
                }


            }

        }

  }

}

阅读 348

收藏
2020-06-26

共1个答案

一尘不染

终于这对我有用。元素当前不可见,因此可能无法与之交互。

最初,这就像测试只成功了5次中的2次一样。不确定有时如何运作,其他人则无法运作。

通过减少IE中的安全设置来工作。启用所有activeX控件。同时启用脚本和IFRAMES。其中一些将警告您将计算机置于危险之中,但这是我唯一的解决方案。通过在页面加载花费时间的每个点上使用presenceOfElementLocated而不是visibleOfElementLocated来引入显式等待。

    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*/
2020-06-26