簡體   English   中英

使用 Python selenium 單擊表格內的按鈕

[英]Using Python selenium to click button inside the table

我試圖從網站上提取數據,這里:

https://apps.ecology.wa.gov/tcpwebreporting/reports/ust?CityZip=Seattle&County=King&StoredSubstance=Unleaded%20Gasoline

我單擊>按鈕以獲取每個加油站的更多詳細信息。 我試圖抓取數據,但找不到使用我的代碼單擊>按鈕的方法。

我能夠提取每一行的元素。 我接下來該怎么做?

driver = webdriver.Chrome(executable_path=r'C:\Users\Owner\Desktop\Career\Coltura\chromedriver.exe')
driver.get('https://apps.ecology.wa.gov/tcpwebreporting/reports/ust?CityZip=Seattle&County=King&StoredSubstance=Unleaded%20Gasoline')
buttons = driver.find_elements_by_class_name(' details-control parent-td clickable parent-control')
driver.find_elements_by_tag_name('tr')
<tr class="clickable odd details" role="row">
    <td class=" details-control parent-td clickable parent-control">
        <button title="Toggle more information about the site RICK'S CHEVRON GROCERY" class="btn btn-sm btn-whitesmoke"></button>
    </td>
    <td class=" parent-td">27</td>
    <td class=" parent-td">41179492</td>
    <td class=" parent-td">A3602</td>
    <td class=" parent-td">RICK'S CHEVRON GROCERY</td>
    <td class=" parent-td">8506 5TH AVE NE</td>
    <td class=" parent-td">Seattle</td>
    <td class=" parent-td">98115</td>
    <td class=" parent-td">King</td>
    <td class=" parent-td">Northwest</td>
</tr>

要定位具有多個類名的元素,您可以使用*_by_css_selector而不是_by_class_name

我建議在單擊元素之前使用方法: .location_once_scrolled_into_view

這是單擊您的意思的每個箭頭按鈕:

driver.get('https://apps.ecology.wa.gov/tcpwebreporting/reports/ust?CityZip=Seattle&County=King&StoredSubstance=Unleaded%20Gasoline')

#add some wait here.....

arrows = driver.find_elements_by_css_selector('td[class*="details-control"]')
for arrow in arrows:
    arrow.location_once_scrolled_into_view
    time.sleep(0.5)
    arrow.click()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM