【发布时间】:2020-03-12 03:19:10
【问题描述】:
我正在处理分页,并希望我的脚本抓取一个表格,点击下一个按钮,抓取下一个表格,然后点击下一个,直到它不再可点击。
可点击与不可点击的唯一区别似乎是 disabled> 结束标签。
我的想法是创建一个while循环并单击按钮直到禁用的标签消失,但我不知道如何首先获得该标签。
即使按钮被禁用,Selenium 也不会抛出“元素不可交互”错误,所以我认为我不能走这条路。
airport_list = []
fees_list = []
airports = ["https://www.aopa.org/destinations/business/13035#fees", "https://www.aopa.org/destinations/business/35555#fees"]
for a in airports:
driver.get(a)
time.sleep(3)
# Click dropdown
driver.find_element_by_xpath('//div[@class = "mat-select-arrow"]').click()
time.sleep(1)
# Select "All aircraft"
driver.find_elements_by_xpath('//span[@class = "mat-option-text"]')[8].click()
time.sleep(2)
try:
# Check if fees are available
driver.find_element_by_xpath('//mat-row[@class = "mat-row ng-star-inserted"]')
#Scrape each row
fees_table = driver.find_elements_by_xpath('//mat-row[@class = "mat-row ng-star-inserted"]')
for fee in fees_table:
fees_list.append(fee.text)
airport_list.append(a)
#Click on "Next" button
driver.find_elements_by_xpath('//span[@class = "mat-button-wrapper"]')[4].click()
time.sleep(2)
except:
fees_list.append("This location has not disclosed fees or does not charge fees.")
airport_list.append(a)
driver.close()
【问题讨论】:
标签: python selenium web-scraping