Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to use range in a loop: Python

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)

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading