Python selenium: Randomly select from a url list textfile

Viewed 41

I have this textfile (links.txt) containing a list of url (100 urls). I can iterate the url list with the code below and the code currently works as expected. However, i want to randomly iterate only 36 of the urls in the list.

for i in range(20):
    with open("links.txt") as in_file:
         for url in in_file:
              driver.get(url.strip())
            try:
                items1 = driver.find_elements_by_xpath("//div[@class='firms']//div")
                sampled = random.sample(list(items1), 1)
                for items in sampled:
                    items.click()
            except:
                continue
driver.find_element_by_xpath("//input[@value='houses']").click()
1 Answers

This is task for random.sample, you also need to read all lines rather than iterate line-by-line, i.e. replace

for url in in_file:

using

urls = in_file.readlines()
random_urls = random.sample(urls, 36)
for url in random_urls:

Note that order of links inside random_urls might be different than in urls.

Related