【问题标题】:Python: selenium can't read an specific tablePython:硒无法读取特定表
【发布时间】:2021-02-06 16:58:43
【问题描述】:

我正在尝试从 this table 读取和获取元素的 href,但我不能尝试 selenium 和 urllib 库,但都不起作用:

driver = webdriver.Chrome('C:/Users/Public/chromedriver')
driver.get(str(link))
driver.implicitly_wait(30)
time.sleep(10)
try:
    element = WebDriverWait(driver, 50).until(
        EC.presence_of_element_located((By.CLASS_NAME, "tabla_datos_linea"))
    )
    for a in driver.find_elements_by_partial_link_text('impacto'):
        print(a.text)
        
    
finally:
    driver.quit()

我在这段代码中夸大了用于等待 DOM 通过显式和隐式等待加载的条件,但仍然没有占用表格 :(。 这段代码的输出是:

Expediente de evaluación de impacto ambiental

但我想要这个:

Expediente de evaluación de impacto ambiental
Estudio de impacto ambiental Firmado con certificado digital acreditado

有谁知道这张桌子有什么技巧吗?我在其他时候使用硒没有问题。而且那个地方是公开的,所以我认为它没有一些障碍或其他东西。

先谢谢了!

【问题讨论】:

  • 你确定你不是最重要的吗? (xpath to it -> /html/body/div/div[2]/table/tbody/tr/td/div[1]/ul/li[2]/a/em) ...我建议使用 for 循环,它将使用 xpath 遍历该表...但在我看来...您永远不会得到那些由 DOM 加载的数据.. . 我建议你尝试使用一些 JS 脚本(我还没有看到 selenium 正在检查 JS 相关的东西......据我所知,它只是将 HTML 内容作为局部变量抓取并遍历它......但我可能是错的)祝你好运......我会得到那个 XPath :)

标签: python selenium web-scraping css-selectors webdriverwait


【解决方案1】:

iframe 中的内部表,您需要先切换到iframe 才能访问元素。

driver.get("https://seia.sea.gob.cl/expediente/expedientesEvaluacion.php?modo=ficha&id_expediente=2148718463")
elements=WebDriverWait(driver,20).until(EC.presence_of_all_elements_located((By.CSS_SELECTOR,".tabla_datos_linea ul.yui-nav a")))
for element in elements[1:]:
    print(element.text)
    print(element.get_attribute("href"))

WebDriverWait(driver,20).until(EC.frame_to_be_available_and_switch_to_it((By.ID,"miFrame")))
elementsinnertable=WebDriverWait(driver,20).until(EC.presence_of_all_elements_located((By.CSS_SELECTOR,".tabla_datos_linea a[href^='https://seia.sea.gob.cl/documentos/documento.php']")))
for a in elementsinnertable:
    print(a.text)
    print(a.get_attribute("href"))

控制台输出:

Expediente de evaluación de impacto ambiental
https://seia.sea.gob.cl/expediente/expedientesEvaluacion.php?modo=ficha&id_expediente=2148718463#-1
Participación ciudadana
https://seia.sea.gob.cl/expediente/expedientesEvaluacion.php?modo=ficha&id_expediente=2148718463#-1
Carta de envío texto radiodifusión
https://seia.sea.gob.cl/documentos/documento.php?idDocumento=2148718469
Extracto
https://seia.sea.gob.cl/documentos/documento.php?idDocumento=2148718470
Estudio de impacto ambiental
https://seia.sea.gob.cl/documentos/documento.php?idDocumento=2148718467
Resolución de Admisibilidad
https://seia.sea.gob.cl/documentos/documento.php?idDocumento=2148822701
Notificación de documento
https://seia.sea.gob.cl/documentos/documento.php?idDocumento=2148824474
Solicitud de evaluación de EIA
https://seia.sea.gob.cl/documentos/documento.php?idDocumento=2148834061
Solicitud de evaluación de EIA a gobierno regional
https://seia.sea.gob.cl/documentos/documento.php?idDocumento=2148834086
Solicitud de evaluación de EIA a municipalidad
https://seia.sea.gob.cl/documentos/documento.php?idDocumento=2148834093

【讨论】:

    猜你喜欢
    • 2021-12-21
    • 2018-07-26
    • 1970-01-01
    • 1970-01-01
    • 2016-03-21
    • 2020-07-02
    • 1970-01-01
    • 1970-01-01
    • 2015-09-03
    相关资源
    最近更新 更多