匹配所有 <td>使用 Webdriver Selenium - Python 的特定原始表 [英] Match all <td> of a specific raw table with Webdriver Selenium - Python

查看:14
本文介绍了匹配所有 <td>使用 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.

推荐答案

创建一个包含所有 及其子 的列表文本 bus 您可以使用以下 定位策略:

To create a list of all the <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策略:


Ideally you need to induce WebDriverWait for the 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')]]")))

注意:您必须添加以下导入:

Note : You have to add the following imports :

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

这篇关于匹配所有 &lt;td&gt;使用 Webdriver Selenium - Python 的特定原始表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆