selenium - 如何从动态网页中提取信息
问题描述
单击查看详细信息的绿色按钮后,我会弹出显示更多信息的窗口。我怎样才能刮掉那个弹出窗口中的信息。单击使用 selenium 后,我尝试提取信息,但我收到此错误
解决方案
您在那里有一个 iframe,它会导致问题:您可以从该弹出窗口中获取数据,如下所示:
from selenium import webdriver
import time
driver = webdriver.Chrome(executable_path="D:/chromedriver.exe")
driver.get('http://www.directoriomedicomexicano.com/Esp_Medicas/MedicosListado.aspx?P1=37&P2=ORTOPEDIA%20Y%20TRAUMATOLOGIA')
#get all 'Ver detalles'
view_more_buttons = driver.find_elements_by_xpath("//img[contains(@onclick,'ShowWindow')]")
#click on the first one
view_more_buttons[0].click()
#switch to the iframe
iframe = driver.find_element_by_id("ctl00_ContentPlaceHolder1_ASPxPopupControl1_CIF1")
driver.switch_to.frame(iframe)
#wait 3 seconds
time.sleep(3)
#get the text from the table
table = driver.find_element_by_xpath("//table[@align='center']")
print(table.text)
推荐阅读
- javafx - 如何打印 TextArea 的内容?
- angular - Angular Schematics - 如何动态创建文件?
- opencv - 使用 OpenCV 将图像保存到我选择的另一个文件夹(带有“~”)
- php - 有没有办法从代码中导入 json 模板文件并在带有 Elementor 的页面上使用它们?
- java - 如何编写java关闭连接?
- xsd - 使用来自另一个 xsd 的选择性元素替换来解组 xsd
- c++ - 使用 QFileDialog 程序几秒钟后崩溃
- c# - 使用 JSON.NET 生成截断的 JSON
- utf-8 - Unicode 和 ASCII 运算符之间的区别
- python - 在 TabbedPanel 中获取小部件/项目 ID