每次網頁有變化時,我都想獲取網頁的文本元素。因此,對于具有文本元素,這是我的方法:
public void getContentPage(WebDriver driver) {
WebDriverWait wait = new WebDriverWait(driver, 15);
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.tagName("body")));
System.out.println(element.getText());
}
我需要的是一種使用 Selenium 的偵聽器,每次 HTML 正文內容發生更改時都會呼叫上述方法:
public void listen (WebDriver driver) {
// some kind of listner that waits for any changes to happen in HTML
if (changed) getContentPage(driver);
else keeplistning()
}
uj5u.com熱心網友回復:
我不確定是否有一種方法可以跟蹤頁面上的所有更改,我不確定您是否需要這樣做,因為這會觸發您進行許多不相關的更改。
這里有用的是跟蹤某些特定相關元素的更改。
因此,要等到某些特定元素發生更改,您可以使用refreshed ExpectedCondition如下所示:
WebElement button = driver.findElement(By.id("myBtn"));
wait.until(ExpectedConditions.refreshed(button));
如果您希望監視多個元素,它將是這樣的:
wait.until(ExpectedConditions.or(
ExpectedConditions.refreshed(element1),
ExpectedConditions.refreshed(element2),
ExpectedConditions.refreshed(element3)));
您當然應該根據您的特定代碼使用將其包裝在某種方法中。我在這里只寫了基本的想法。
UPD
要跟蹤整個頁面,您可以使用driver.getPageSource();方法。以一定的時間間隔輪詢頁面狀態并將此方法的先前結果的值與新結果進行比較,將為您提供任何頁面內容更改的指示。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/385888.html
下一篇:使用硒時不列印文本
