使用Python selenium单击iframe元素

时间:2014-05-15 20:45:03

标签: iframe selenium python-3.x cross-domain

如何使用selenium python点击主机A(在此示例中为jsfiddle.net)中的红色按钮here? (java脚本限制策略错误不要让我这样做)。我也不想直接点击红色按钮。 感谢。

  

el = driver.find_element_by_xpath(“/ html / body / div 2 / input”)
  webdriver.ActionChains(驱动程序).move_to_element致发光(EL)。单击(EL).perform()

保持红色按钮的 iframe:

<iframe src="http://www.myhostb.blogfa.com/" width="500" height="300">
</iframe>

重要提示:jsfiddle.net是主机A的成像。

DEMO

1 个答案:

答案 0 :(得分:7)

您需要使用switch_to_frame()

driver.switch_to_frame("result")
driver.switch_to_frame(driver.find_element_by_css_selector("body>iframe"))
driver.find_element_by_css_selector("input.ex2").click()

完成iframe后,您可以使用以下方式切换回顶部框架:

driver.switch_to_default_content()