Python Selenium driver.get() not working within a for loop
Question:
The code below logs into a YouTube account, and once logged in, it should visit a few YouTube videos.
The issue is:
-
If I do a simple direct link like here, it works
driver.get('https://www.youtube.com/watch?v=FFDDN1C1MEQ')
-
If I do a loop to visit multiple links i get an error:
raise exception_class(message, screen, stacktrace)
InvalidArgumentException: invalid type: sequence, expected a string at line 1 column 8
the full code is below
import time
import numpy as np
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import NoSuchElementException
def youtube_login():
email = '[email protected]'
password = 'emailPassword'
# Browser
driver = webdriver.Firefox()
driver.get('https://accounts.google.com/ServiceLogin?hl=en&continue=https%3A%2F%2Fwww.youtube.com%2Fsignin%3Fhl%3Den%26feature%3Dsign_in_button%26app%3Ddesktop%26action_handle_signin%3Dtrue%26next%3D%252F&uilel=3&passive=true&service=youtube#identifier')
# log in
driver.find_element_by_id('identifierId').send_keys(email)
driver.find_element_by_class_name('CwaK9').click()
WebDriverWait(driver, 500).until(EC.element_to_be_clickable((By.NAME, "password")))
driver.find_element_by_name('password').send_keys(password)
driver.find_element_by_class_name('CwaK9').click()
WebDriverWait(driver, 500).until(EC.element_to_be_clickable((By.ID, "identity-prompt-confirm-button")))
driver.find_element_by_id('identity-prompt-confirm-button').click()
#driver.get('https://www.youtube.com/watch?v=FFDDN1C1MEQ') # If I do a simple direct link like here, it works
urls = []
# You can add in a file and import from there
inp = open ("urls.txt","r")
for line in inp.readlines():
urls.append(line.split())
for url in urls:
driver.get(url)
youtube_login()
Answers:
I think you have bad URL format in urls.txt
Try to debug URL like this:
from selenium.common.exceptions import InvalidArgumentException
for url in urls:
try:
driver.get(url)
except InvalidArgumentException:
print(url)
I solved this with import time, time.sleep(0.5) before driver.get().
The code below logs into a YouTube account, and once logged in, it should visit a few YouTube videos.
The issue is:
-
If I do a simple direct link like here, it works
driver.get('https://www.youtube.com/watch?v=FFDDN1C1MEQ')
-
If I do a loop to visit multiple links i get an error:
raise exception_class(message, screen, stacktrace)
InvalidArgumentException: invalid type: sequence, expected a string at line 1 column 8
the full code is below
import time
import numpy as np
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import NoSuchElementException
def youtube_login():
email = '[email protected]'
password = 'emailPassword'
# Browser
driver = webdriver.Firefox()
driver.get('https://accounts.google.com/ServiceLogin?hl=en&continue=https%3A%2F%2Fwww.youtube.com%2Fsignin%3Fhl%3Den%26feature%3Dsign_in_button%26app%3Ddesktop%26action_handle_signin%3Dtrue%26next%3D%252F&uilel=3&passive=true&service=youtube#identifier')
# log in
driver.find_element_by_id('identifierId').send_keys(email)
driver.find_element_by_class_name('CwaK9').click()
WebDriverWait(driver, 500).until(EC.element_to_be_clickable((By.NAME, "password")))
driver.find_element_by_name('password').send_keys(password)
driver.find_element_by_class_name('CwaK9').click()
WebDriverWait(driver, 500).until(EC.element_to_be_clickable((By.ID, "identity-prompt-confirm-button")))
driver.find_element_by_id('identity-prompt-confirm-button').click()
#driver.get('https://www.youtube.com/watch?v=FFDDN1C1MEQ') # If I do a simple direct link like here, it works
urls = []
# You can add in a file and import from there
inp = open ("urls.txt","r")
for line in inp.readlines():
urls.append(line.split())
for url in urls:
driver.get(url)
youtube_login()
I think you have bad URL format in urls.txt
Try to debug URL like this:
from selenium.common.exceptions import InvalidArgumentException
for url in urls:
try:
driver.get(url)
except InvalidArgumentException:
print(url)
I solved this with import time, time.sleep(0.5) before driver.get().