如何在Selenium WebDriver中获取Inspect Element代码

use*_*522 7 selenium selenium-webdriver

我在Firefox浏览器中使用selenium.

View Source(CTRL + U)中显示的Html代码与我在检查Firefox中的元素时看到的html代码不同.

当我运行driver.getPageSource()我只获得查看源(CTRL + U)代码.

有没有办法访问Inspect元素代码而不是查看源代码

小智 7

我想你的问题在这里得到解答.

View Source html是服务器发送的内容.我认为它是编译时html,或DOM的初始状态.

Inspect Element html可能已经被ajax响应或javascript更新,因此不一定是相同的.我认为它是运行时html,或DOM的当前状态.

GetAttribute()方法查询当前的DOM元素状态.您可以直接返回特定的html属性值

webElement.GetAttribute("class")
Run Code Online (Sandbox Code Playgroud)

或者获取整个html字符串.

webElement.GetAttribute("innerHTML")
Run Code Online (Sandbox Code Playgroud)

  • 我也有同样的问题,页面只出现简单的。我只能在检查器中看到的高级代码。有没有人设法解决这个问题? (2认同)