Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
300 views
in Technique[技术] by (71.8m points)

Match all <td> of a specific raw table with Webdriver Selenium - Python

I'm still new in web scrapping and I have this question related to Webdriver.

Code Exemple :

<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>

So the idea is that in my original code, I have multiple tables with the same ID and class names.

Question : How can i proceed to extract all the TRs that contains the word "bus".

I can't find the correct xpath syntax to use.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

To create a list of all the <tr> with their child <td> containing the text bus you can use the following based Locator Strategies:

elements = driver.find_elements_by_xpath("//tr[.//td[contains(., 'bus')]]")

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...