[英]How to get the second block of html code with selenium
我的代码:
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>
的第二块
谢谢!
看起来您选择了您想要的<td>
的父元素。 只需使用 css 选择器来获取其中的所有 td 元素:
a=driver.find_elements_by_css_selector('#rId_48 td')
注意find_elements...
的“s”。 这将返回所有<td>
元素的列表。 所以你想要的应该是a[1]
。
如果要定位特定的行和单元格值,可以使用以下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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.