【问题标题】:Getting html elements of another page with window.open()使用 window.open() 获取另一个页面的 html 元素
【发布时间】:2013-04-26 12:16:20
【问题描述】:

我正在尝试从我使用 window.open() 访问的 html 页面访问 html 元素。

这些是我的 html:

首页.html:

<html>
<body>
<script>
function openPage() {
  return window.open("secondpage.html")
}
</script>
</body>
</html>

secondpage.html

<html>
<body>
<h1>Hello!</h2>
<p>This is the second page</p>
</body>
</html>

这就是我正在做的:

from selenium import webdriver
browser = webdriver.Firefox()
browser.get("firstpage.html")
html = browser.execute_script("return openPage().document;")
print html

我期望得到的是对第二页文档元素的引用。这似乎适用于 Firefox Web 控制台。当我测试脚本时,第二页打开,但第一页似乎挂起,过了一会儿我得到一个对话框:

“此页面上的某个脚本可能正忙,或者它可能已停止响应。您可以立即停止该脚本,也可以继续查看该脚本是否会完成。”

使用“停止”和“继续”按钮。按“继续”对话框不断出现,当我最终按“停止”时,html python 变量包含与对话框相同的文本。

我做错了什么?

编辑: 正如@e1che 的回答一样,这是正确的做法:

首页.html:

<html>
<body>
<script>
function openPage() {
  window.open("secondpage.html", "secondpagewindow")
}
</script>
</body>
</html>

python代码:

from selenium import webdriver
browser = webdriver.Firefox()
browser.get("firstpage.html")
browser.execute_script("openPage()")
browser.switch_to_window("secondpagewindow")
print browser.page_source

【问题讨论】:

    标签: python firefox selenium-webdriver


    【解决方案1】:

    你做得很好,

    但您错过了将驱动程序切换到新窗口。

    所以就在你browser.execute_script("return openPage().document;")之后

    你写了这样的东西:

    driver.switch_to_window("windowName")
    

    我让你搜索here for more infos and tricks ;)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-06-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多