Skip to content
Advertisement

How to open multiple sites in Selenium with user input?

I’m fairly new to coding with Python and I had some questions regarding Selenium:

So basically what I’m trying is:

-user inputs multiple links to different sites

-the script opens the first link and closes the browser afterwards and opens the next link

-the user only inputs for example ‘google.com’ but the browser automatically opens ‘http://google.com’ (thats the reason why I try to append the link and the ‘http’ variable)

but I always have the following error code: line 18, in <module> driver.get(url) and Chrome opens a new tab with ‘data:,’ as the URL

from selenium import webdriver
from time import sleep


http = list()
http = ['http://']

input_1 = input('Site: ').split()

link = http.append(input_1) 

driver = webdriver.Chrome(executable_path=r'C:...chromedriver.exe')

for url in link:
    driver.set_window_size(1920, 1080)
    driver.get(url)
    sleep(4)
driver.quit()

Advertisement

Answer

Your logic/syntax is wrong. You have to fix the way you are attaching the ‘http://’ part. Following code is working for me:

from selenium import webdriver from time import sleep

http = 'http://'
input_1 = input('Site: ').split()
link = [http + site for site in input_1]

driver = webdriver.Chrome(executable_path=r'C:...chromedriver.exe')

for url in link:
    driver.set_window_size(1920, 1080)
    driver.get(url)
    sleep(4)
driver.quit()
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement