2015-03-02 128 views
2

这里是我的代码,请单击下面的错误在这Website硒的webdriver:元素不可见异常

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(); 

    } 
} 

我得到一个简单的登录按钮:

Exception in thread "main" org.openqa.selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with Command duration or timeout: 2.05 seconds

回答

9

你有定XPath两个按钮在这个页面上,首先不可见,那就是为什么你会得到ElementNotVisibleException

其中一个是<div class="loginPopup">

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

因此改变你的XPath看起来像这样,它会解决您的问题:

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

甚至有3个元素与id="_loginButton"在页面上

By.cssSelector("form#_loginForm div#_loginButton") 
: - ,和 只有一个可视一个位于登录表单里面,你可以通过 CSS选择器得到它0
1

发生了3次id="_loginButton"

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

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

Webdriver可能抛出异常ElementNotVisible在-情况下,存在具有相同的定位器的多个元素,并且如果Webdriver在匹配定位器的元件的一个已经操作。

在这种情况下,您可以使用

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

首先获得该元素的大小,然后从列表中采取的第一个元素,然后单击元素。

driver.findElements(By.xpath("locator")).get(var_ele_size-1).click(); 
0
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()); 
       } 


     } 
    } 
} 
+0

我收到错误“org.openqa.selenium.ElementNotVisibleException:元素不可见” 需要帮助 – 2017-09-10 03:48:39

+0

仍然同样的问题: - 你可以请h请帮我一下吗? http://www.kiryanaworld.com/ – 2017-09-10 13:57:51

-1

确保您的remote server窗口足够大,因此元素不隐藏,因为空间的限制..

这为我工作:(我用c#

driver.Manage().Window.Size = new System.Drawing.Size(1928, 1060); 
相关问题