Loop through links using Selenium Webdriver (Python)

后端 未结 1 811
一整个雨季
一整个雨季 2020-12-11 10:12

Afternoon all. Currently trying to use Selenium webdriver to loop through a list of links on a page. Specifically, it\'s clicking a link, grabbing a line of text off said pa

相关标签:
1条回答
  • 2020-12-11 10:18

    I'm not sure if this will fix the problem, but in general it is better to use WebDriverWait rather than implicitly_wait since WebDriveWait.until will keep calling the supplied function (e.g. driver.find_element_by_xpath) until the returned value is not False-ish or the timeout (e.g 5000 seconds) is reached -- at which point it raises a selenium.common.execptions.TimeoutException.

    import selenium.webdriver.support.ui as UI
    
    def test_text_saver(self):
        driver = self.driver
        wait = UI.WebDriverWait(driver, 5000)
        with open("textsave.txt","w") as textsave:
            list_of_links = driver.find_elements_by_xpath("//*[@id=\"learn-sub\"]/div[4]/div/div/div/div[1]/div[2]/div/div/ul/li/a")
            for link in list_of_links:  # 2
                link.click()   # 1
                text = wait.until(
                    lambda driver: driver.find_element_by_xpath("//*[@id=\"learn-sub\"]/div[4]/div/div/div/div[1]/div[1]/div[1]/h1").text)
                textsave.write(text+"\n\n")
                driver.back()
    
    1. After you click the link, you should wait until the linked url is loaded. So the call to wait.until is placed directly after link.click()
    2. Instead of using

      while x <= link_count:
          ...
          x += 1
      

      it is better to use

      for link in list_of_links: 
      

      For one think, it improves readability. Moreover, you really don't need to care about the number x, all you really care about is looping over the links, which is what the for-loop does.

    0 讨论(0)
提交回复
热议问题