首页 > 解决方案 > 如何使用 selenium 获取第二个 html 代码块

问题描述

我的代码:

from selenium import webdriver
driver.get('http://www.datiopen.it/it/opendata/Mappa_delle_stazioni_ferroviarie_in_Italia')
element = driver.find_element_by_id("Tabella")
time.sleep(5)
element.click()
time.sleep(5)
a=driver.find_element_by_id('rId_48').get_attribute('innerHTML')
print(a)

我的输出:

<td role="gridcell" style="" title="" aria-describedby="list_"><a title="Vedi su Google Maps" href="javascript:StatPortalOpenData.ODataUtility.openInStreetView(45.0760003999999,7.5911782);"><img alt="Vedi su Google Maps" height="25" width="25" style="vertical-align:middle" src="/sites/all/modules/spodata/metadata/viewer/multidimensional_viewer/img/streetView.png"></a></td>

<td role="gridcell" style="" class="" title="COLLEGNO" aria-describedby="list_Cccomune_608711150">COLLEGNO</td>

我想要的输出:

<td role="gridcell" style="" class="" title="COLLEGNO" aria-describedby="list_Cccomune_608711150">COLLEGNO</td><td role="gridcell" style="" class="" title="CITTA' METROPOLITANA DI TORINO" aria-describedby="list_Ccprovincia_1472723626">CITTA' METROPOLITANA DI TORINO</td>

所以这是第二块<td> </td>

谢谢!

标签: pythonselenium

解决方案


如果要针对特定​​的单元格值,可以使用以下CSS selector.

print the element

print(driver.find_element_by_css_selector("#rId_48>td:nth-child(2)").get_attribute('outerHTML'))
print(driver.find_element_by_css_selector("#rId_48>td:nth-child(3)").get_attribute('outerHTML'))

或者to print the text of the element

print(driver.find_element_by_css_selector("#rId_48>td:nth-child(2)").text)
print(driver.find_element_by_css_selector("#rId_48>td:nth-child(3)").text)

推荐阅读