【发布时间】:2021-08-02 13:50:20
【问题描述】:
我要爬网,结构如下:
<iframe>
#document
<html>
......
</html>
</iframe>
我需要进入“html”并单击按钮,但我找不到进入的方法。
有什么方法可以点击“#tag_name”里面的按钮吗?
【问题讨论】:
标签: python selenium web-crawler
我要爬网,结构如下:
<iframe>
#document
<html>
......
</html>
</iframe>
我需要进入“html”并单击按钮,但我找不到进入的方法。
有什么方法可以点击“#tag_name”里面的按钮吗?
【问题讨论】:
标签: python selenium web-crawler
要访问 iframe 中的元素,您必须切换到该 iframe。
如果这是页面上唯一可以使用的 iframe:
driver.switch_to.frame(driver.find_element_by_xpath('//iframe'))
如果那里有多个 iframe,您应该与其他网络元素类似地定位它们
【讨论】:
这与我们为其他标签抓取 DOM 的方式非常相似。
<html>
<body>
....
</body>
</html>
我们通常写成"//div[@tag-name="<<value>>"]"。非常相似,我们也执行帧。
我们不是直接写来访问元素,而是切换到框架然后访问如下
driver.switch_to_frame("<<frame id or frame name>>")
driver.find_element(By.xpath, '//div[@tag-name="<<value>>"]').click()
driver.switch_to.default_content()
或
driver.switch_to_frame("<<frame id or frame name>>")
element = driver.find_element_by_xpath("//div[@tag-name="<<value>>"]")
element.click()
driver.switch_to.default_content()
【讨论】: