org.openqa.selenium.ElementNotVisibleException:元素当前不可见,因此可能不会与 [英] org.openqa.selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with

查看:147
本文介绍了org.openqa.selenium.ElementNotVisibleException:元素当前不可见,因此可能不会与的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试执行以下Selenium Web驱动程序脚本,但是我正在获得 org.openqa.selenium.ElementNotVisibleException:元素当前不可见,因此可能不会与错误几次(不是所有的时间)。有时在循环中首先迭代,有时在2次迭代中,有时没有启动循环。它打印所有可用的项目正确计数,但是尝试点击项目,它显示元素当前不可见...

  public void pickitems()throws异常
{
Webdriver driver = new firefoxdriver();
driver.get(http://www.bigbasket.com);
driver.manage()。window()。maximize();
//选择位置
列表< WebElement> list = driver.findElement(By.id(ftv-city-popup))findElements(By.tagName(button));
int location = r.nextInt(list.size());
list.get(location).click();
//从左侧面板中选择随机类别列表
Thread.sleep(30000);
列表< WebElement> xyz = driver.findElement(By.id(uiv2-main-menu))findElements(By.className(top-category));
System.out.println(xyz.size());
随机r = new Random();
int category = r.nextInt(xyz.size());
xyz.get(category).click(); (int i = 0; i< 3; i ++){
Thread.sleep(30000);



列表< 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);
列表< WebElement> availableItems2 = driver.findElements(By.cssSelector(a.uiv2-add-button.a2c));
if(availableItems2.size()== 0){
System.out.println(没有更多的项目可用,不好意见);
}
else {
Assert.fail(无法选择项目,可能是页面加载问题);
}


}

}

}

}


解决方案

最后,这对我有用。
元素当前不可见,因此可能无法与之进行交互。



最初只是测试成功只有5次。不确定它是如何工作的,而其他人则不行。



通过减少IE中的安全设置来实现。启用所有activeX控件。启用脚本和IFRAMES。其中有些会警告计算机处于危险之中,但这是我唯一的解决方案。在页面加载需要时间的每个时刻,使用presenceOfElementLocated而不是visibilityOfElementLocated引入显式等待。

  WebDriverWait wait = new WebDriverWait(driver, 30); 
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(// * [@ id ='campaignListTable']))); / *检查搜索的xpath
box * /
driver.findElement(By.xpath(// * [@ id ='campaignListTable']))。sendKeys(TEXT); / *输入搜索文本
box * /


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...

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


            }

        }

  }

}

解决方案

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.

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.

    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*/

这篇关于org.openqa.selenium.ElementNotVisibleException:元素当前不可见,因此可能不会与的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆