簡體   English   中英

在網頁未完全加載時,如何強制WebDriver執行命令?

[英]How to force WebDriver to execute a command while the webpage is not loaded completely?

網頁從上到下加載。

1) <html ...
2) <head ...
3) <body ...
  etc

我需要WebDriver明確等待,直到<title標記可見。 然后閱讀標題的內容,並繼續執行其他操作, 而無需等待整個頁面被加載!!!

WebDriver driver = new ChromeDriver();
driver.get("http://");
WebDriverWait wait = new WebDriverWait(driver, 0) // This line is probably the one to be transformed in order to correspond to my requirements
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//title")));
// The problem is that the following commands do not start off until the whole page is loaded
if(driver.getTitle().equals("whatever")) {
driver.get("http://");
}
else {
...
}

您可以使用pageLoadTimeout 將超時設置為0 ,然后捕獲TimeoutException ,然后執行特定的Title斷言等。

driver.manage().timeouts().pageLoadTimeout(0, TimeUnit.SECONDS);

UPDATE

driver.manage().timeouts().pageLoadTimeout(0, TimeUnit.SECONDS);
WebDriverWait wait = new WebDriverWait(driver, 30);
try {
    driver.get("http://");
} catch (TimeoutException e) {
    wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//title")));
}

欲了解更多信息

理想情況下,您應該使用ExpectedConditions.titleIs()有關可用選項的更多信息,可在此處找到。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM