匹配所有 <td>使用 Webdriver Selenium - Python 的特定原始表 [英] Match all <td> of a specific raw table with Webdriver Selenium - Python
问题描述
我还是网络抓取的新手,我有这个与 Webdriver 相关的问题.
I'm still new in web scraping and I have this question related to Webdriver.
代码示例:
<table>
<tbody>
<tr>
<td> car </td>
<td> bus </td>
</tr>
<tr>
<td> car </td>
<td> bus & train </td>
</tr>
<tr>
<td> car </td>
<td> bus & plane </td>
</tr>
</tbody>
</table>
<table>
<tbody>
<tr>
<td> food </td>
<td> meat</td>
</tr>
<tr>
<td> drink </td>
<td> water </td>
</tr>
</tbody>
</table>
所以想法是,在我的原始代码中,我有多个具有相同 ID 和类名的表.
So the idea is that in my original code, I have multiple tables with the same ID and class names.
问题:如何提取所有包含bus"一词的TR.
Question : How can i proceed to extract all the TRs that contains the word "bus".
我找不到要使用的正确 xpath 语法.
I can't find the correct xpath syntax to use.
推荐答案
创建一个包含所有 To create a list of all the 理想情况下,您需要为
Ideally you need to induce WebDriverWait for the 注意:您必须添加以下导入: Note : You have to add the following imports : 这篇关于匹配所有 <td>使用 Webdriver Selenium - Python 的特定原始表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! 及其子 的列表文本 bus 您可以使用以下 基于 xpath 的定位策略:
<tr>
with their child <td>
containing the text bus you can use the following xpath based Locator Strategies:elements = driver.find_elements_by_xpath("//tr[.//td[contains(., 'bus')]]")
引入 WebDriverWaitvisibility_of_all_elements_located()
并且您可以使用以下任一Locator策略:
visibility_of_all_elements_located()
and you can use either of the following Locator Strategies:elements = WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//tr[.//td[contains(., 'bus')]]")))
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
登录
关闭