[英]Selenium Webdriver: Element Not Visible Exception
這是我的代碼,點擊本網站上的簡單登錄按鈕
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();
}
}
我收到以下錯誤:
線程“main”中的異常org.openqa.selenium.ElementNotVisibleException:元素當前不可見,因此可能無法與命令持續時間或超時交互:2.05秒
你在這個頁面上有兩個帶有給定xpath的按鈕,首先是不可見的,這就是你得到ElementNotVisibleException的原因
一個在<div class="loginPopup">
第二個(你需要的那個)在<div class="page">
因此,將您的xpath更改為如下所示,它將解決您的問題:
By.xpath("//div[@class='page']//div[@id='_loginButton']")
頁面上甚至有3個元素id="_loginButton"
, 只有一個可見 - 位於登錄表單內的那個元素,你可以通過CSS選擇器獲取它:
By.cssSelector("form#_loginForm div#_loginButton")
有3次出現id="_loginButton"
。
在cssSelector的class="signIn"
id="_loginButton"
下使用id="_loginButton"
來獲取頁面中的確切按鈕。
By.cssSelector("div.signIn div#_loginButton")
如果有多個元素具有相同的定位器,並且Webdriver
已經在匹配定位器的元素之一上操作,則Webdriver
可能會拋出ElementNotVisible
異常。
在這種情況下,您可以先使用獲取元素的大小
int var_ele_size= driver.findElements(By.xpath("locator")).size();
然后從列表中獲取第一個元素並單擊該元素。
driver.findElements(By.xpath("locator")).get(var_ele_size-1).click();
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());
}
}
}
}
確保remote server
上的窗口足夠大,以免因空間限制而隱藏元素。
這對我有用:(我用c#
)
driver.Manage().Window.Size = new System.Drawing.Size(1928, 1060);
你可以嘗試:
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("your locator value")));
要么
wait.until(ExpectedConditions.ElementIsVisible(By.xpath("your locator value")));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.