2015-03-02 132 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); 
相關問題