【问题标题】:How to get the closing tag in selenium如何在硒中获取结束标签
【发布时间】: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


    【解决方案1】:

    我能够从表格底部提取最大数量的项目,将该数字除以 10,然后四舍五入到最接近的数字。然后我使用该数字遍历一个范围。

    airport_list = []
    fees_list = []
    
    airports = ["https://www.aopa.org/destinations/business/13035#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"]')
    
            # Get number of items
            number_of_items = driver.find_element_by_xpath('//div[@class = "mat-paginator-range-label"]').text.split()[-1]
            #print(number_of_items)
    
            if float(number_of_items) >= 11:
                number_of_button_clicks = math.ceil(float(number_of_items)/10)
            else:
                number_of_button_clicks = 0
            #print(number_of_button_clicks)
    
            for click in range(0, number_of_button_clicks):
                #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)
    
    
    #print(fee_list)
    #print(airport_list)            
    
    driver.close()
    

    【讨论】:

      【解决方案2】:

      使用如下代码所示的最大行数限制,而不是继续下一页。最重要的是,你真的不需要使用 try except 块 -

      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(3)
      
          # select 100 Items per page if items are present
          if len(driver.find_elements_by_xpath(".//mat-select[@aria-label='Items per page:']")) > 0 :
              driver.find_element_by_xpath(".//mat-select[@aria-label='Items per page:']").click()
              time.sleep(3)
              driver.find_element_by_xpath(".//span[@class='mat-option-text' and text()='100']/parent::mat-option").click()
      
          # 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)
          print(fees_list)
      
              # if needed then Click on "Next" button using this xpath and apply same for loop as above
              #driver.find_elements_by_xpath(".//button[@aria-label='Next page']").click()
      driver.close()
      

      【讨论】:

      • 那行不通,因为有些表包含超过 100 个项目。
      • 我已经给出了,请阅读前面的代码。您可以使用该定位器单击下一步按钮,并将当前的 for 循环逻辑与其他外部 for 循环包装在一起,这样它就可以在后续页面上运行
      猜你喜欢
      • 2015-11-09
      • 2023-03-30
      • 1970-01-01
      • 2019-07-28
      • 1970-01-01
      • 1970-01-01
      • 2018-11-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多