简体   繁体   English

使用 Python selenium 单击表格内的按钮

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

I was trying to extract the data from the website, here:我试图从网站上提取数据,这里:

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

I click the > button to get more details of each gas station.我单击>按钮以获取每个加油站的更多详细信息。 I was trying to scrape the data, but I couldn't find a way to click > button using my codes.我试图抓取数据,但找不到使用我的代码单击>按钮的方法。

I am able to extract each row's elements.我能够提取每一行的元素。 what should I do next?我接下来该怎么做?

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>

To locate element with multiple class name, you can use *_by_css_selector not _by_class_name .要定位具有多个类名的元素,您可以使用*_by_css_selector而不是_by_class_name

I suggest to use method : .location_once_scrolled_into_view before click the element.我建议在单击元素之前使用方法: .location_once_scrolled_into_view

This is for click each arrow button you mean:这是单击您的意思的每个箭头按钮:

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