【问题标题】:Selenium Webdriver: Element Not Visible ExceptionSelenium Webdriver:元素不可见异常
【发布时间】:2015-05-03 10:56:30
【问题描述】:

这是我在Website 上单击简单登录按钮的代码

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;    
import org.openqa.selenium.WebDriver;    
import org.openqa.selenium.firefox.FirefoxDriver;    

public class Reports {

    public static void main(String[] args) {

        WebDriver driver = new FirefoxDriver();
        driver.get("https://platform.drawbrid.ge");
        driver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS);
        driver.findElement(By.xpath(".//*[@id='_loginButton']")).click();

    }
}

我收到以下错误:

线程“主”org.openqa.selenium.ElementNotVisibleException 中的异常:元素当前不可见,因此可能无法与之交互 命令持续时间或超时:2.05 秒

【问题讨论】:

    标签: java selenium selenium-webdriver automation qa


    【解决方案1】:

    你可以试试:

    wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("your locator value")));
    

    或者

    wait.until(ExpectedConditions.ElementIsVisible(By.xpath("your locator value")));
    

    【讨论】:

    【解决方案2】:

    您在此页面上有两个带有给定 xpath 的按钮,第一个不可见,这就是您收到 ElementNotVisibleException 的原因

    <div class="loginPopup">下一个

    第二个(你需要的那个)在<div class="page">

    因此,将您的 xpath 更改为如下所示,它将解决您的问题:

    By.xpath("//div[@class='page']//div[@id='_loginButton']")
    

    【讨论】:

      【解决方案3】:

      确保remote server 上的窗口足够大,这样元素就不会因为空间限制而隐藏..

      这对我有用:(我使用c#

      driver.Manage().Window.Size = new System.Drawing.Size(1928, 1060);
      

      【讨论】:

        【解决方案4】:
        public static void Listget (WebDriver driver) throws Exception 
        
        {
            Thread.sleep(5000);
            UtilityMethod.getAppLocaters(driver, "closeicon").click();
        
            Actions action = new Actions(driver);
            WebElement we = driver.findElement(By.xpath("//li[@class='parent dropdown  aligned-left']"));
            Thread.sleep(5000);
            action.moveToElement(we).build().perform();
        
            List<WebElement>links = driver.findElements(By.xpath("//span[@class='menu-title']"));
            int total_count = links.size();       
            System.out.println("Total size :=" +total_count);           
             for(int i=0;i<total_count;i++)
                {             
                    WebElement  element = links.get(i);
                    String text = element.getAttribute("innerHTML");
                    System.out.println("linksnameis:="  +text);
        
                    try{
                            File src = new File("D:ReadFile.xlsx");
                            FileInputStream fis = new FileInputStream(src);
                            XSSFWorkbook wb=new XSSFWorkbook(fis);
                            XSSFSheet sh = wb.getSheetAt(0);
        
                            sh.createRow(i).createCell(1).setCellValue(text);
        
                            FileOutputStream fos = new FileOutputStream(new File("D:/ReadFile.xlsx"));
                            wb.write(fos);
                            fos.close();
                        }
                        catch(Exception e)
                        {
                            System.out.println(e.getMessage());
                        }
        
        
                }
            }
        }
        

        【讨论】:

        • 我收到错误“org.openqa.selenium.ElementNotVisibleException:元素不可见”需要帮助
        【解决方案5】:

        Webdriver 可能会抛出 ElementNotVisible 异常,以防多个元素具有相同的定位器,并且如果 Webdriver 已经对匹配定位器的元素之一进行了操作。

        在这种情况下,您可以首先使用获取元素的大小

        int var_ele_size= driver.findElements(By.xpath("locator")).size();
        

        然后从列表中取出第一个元素并单击该元素。

        driver.findElements(By.xpath("locator")).get(var_ele_size-1).click();
        

        【讨论】:

          【解决方案6】:

          id="_loginButton" 出现了 3 次。

          通过 cssSelector 使用 class="signIn" 下的 id="_loginButton" 来获取页面中的确切按钮。

          By.cssSelector("div.signIn div#_loginButton")
          

          【讨论】:

            【解决方案7】:

            页面上甚至有 3 个带有id="_loginButton" 的元素,只有一个可见 - 位于登录表单内的那个,您可以通过 CSS 选择器获取它 em>:

            By.cssSelector("form#_loginForm div#_loginButton")
            

            【讨论】:

              猜你喜欢
              • 2018-08-20
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多