Selenium驱动程序不反映单击()后的页面更改

Selenium driver does not reflect page changes after click()

本文关键字:驱动程序 单击 Selenium      更新时间:2023-09-26

以下代码返回的源代码不会反映javascript在调用click()后导致的网页更改。我错过了什么?

driver.findElement(By.xpath(xpath)).click(); // changes page content
Thread.sleep(10000);
String src = driver.getPageSource(); // src does not reflect changes

您不应该依赖于webdrivers getPageSource()方法。

来自文档:

getPageSource

java.lang.String getPageSource()

获取最后加载的页面的源。如果页面已被修改加载后(例如,通过Javascript),不能保证返回的文本是修改后的页面的文本。请查阅用于确定是否返回的文本反映页面或文本的当前状态上次由web服务器发送。返回的页面源是底层DOM的表示:不要期望它被格式化或者以与从web服务器发送的响应相同的方式转义。把它想象成一个艺术家的印象。

返回:当前页面的来源

http://selenium.googlecode.com/git/docs/api/java/org/openqa/selenium/WebDriver.html#getPageSource%28%29