python - 如何使用 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>
谢谢!
解决方案
如果要针对特定的行和单元格值,可以使用以下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)
推荐阅读
- r - 如何比较分类时间序列的三个模型的预测准确性?
- database - 无法在 Visual Studio 2019 中连接数据库
- terraform - Terraform API Gateway HTTP API - 获取错误 Insufficient permissions to enable logging
- highcharts - Highcharts 图例与图形区域重叠
- html - 如何在跨度中对齐角度标签内容
- nlp - 哪个是最好的文章摘要工具?
- java - 第二类是为 TestNG 抛出空指针异常
- validation - 在颤振中无法在验证器中获得正确的值
- extjs - ExtJS 7.3.0 可下载文档?
- mysql - 将 Postgre 查询转换为 Hive/Mysql