1

I'm new to programming and I was just messing around with selenium. I started simple by using it to visit a website. It got me thinking, how would one visit multiple sites, specifically one after the other? How would I go about doing that in Python?

I guess what I'm asking is how would I use selenium to visit a list of sites one after another, waiting about 10 seconds in between going to the sites.

Here's what I have so far:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

driver = webdriver.Firefox(executable_path='PATH_TO_WEBDRIVER')

url = "http://www.google.com"

driver.get(url)
print(driver.title)

2 Answers 2

2

Create a list of urls to visit:

urls = ['http://www.google.com','https://stackoverflow.com']

Loop the list of urls:

for url in urls:
    driver.get(url)
    print(driver.title)
    time.sleep(10)

Example

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time

driver = webdriver.Firefox(executable_path='PATH_TO_WEBDRIVER')
urls = ['http://www.google.com','https://stackoverflow.com']

for url in urls:
    driver.get(url)
    print(driver.title)

driver.close()

Output

Google
Stack Overflow - Where Developers Learn, Share, & Build Careers
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much! This looks like it'll work.
@LearningGuy15 don't forget to close/quite driver
0

To add to HedgeHog's response, should you want to visit websites in succession and the links are not known in advance (example, going to a newspaper and then the link(s) of the first article/first suggested article), I'd follow something like:

driver.get(url)
driver.get(driver.find_element_by_xpath(new_link))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.