Home > Mobile >  How to use range in a loop: Python
How to use range in a loop: Python

Time:12-29

This will print out 10 links, how do I limit it to only fetch the first 3 links?

def getlinks(self):
        self.driver.get('https://www.youtube.com/results?search_query=iphone&sp=EgQIBRAB')

        the_links = self.driver.find_elements(By.ID, "video-title")
        sleep(5)

        for link in the_links:
            self.links.append(link.get_attribute('href'))

        for link in self.links:
            print(link)

CodePudding user response:

Try for link in self.links[:3].

You can also use the equivalent expression for link in self.links[0:3].

  • Related