1

I am writing a Python script for interacting with the Spotify Webplayer.

Initially I have to login by entering the credentials. After I click the login button and I get redirected to the webplayer page. But now if I try to interact with the elements on this page, Python throws an error that the following element is not present.

from selenium import webdriver
driver=webdriver.Chrome(r'C:\Users\admin\Downloads\chromedriver_win32\chromedriver.exe')
driver.get("https://open.spotify.com/collection/playlists")
a = driver.find_element_by_xpath('//button[contains(text(),"Log in")]')
a.click()
driver.find_element_by_id("login-username").send_keys("email")
driver.find_element_by_id("login-password").send_keys("psswd")

ERROR: Message: no such element: Unable to locate element: {"method":"id","selector":"login-username"}

I know that the browser still holds the old page instance, hence it cannot find the required elements. How can I overcome this issue?

1

You cannot delete the cache from the driver but you can delete the cookies, that would log you out out of spotify so that you get the login form.

After that page may still be loading so the element might not be there when you are looking for it, this may be because its created by a script that hasn't finished.

You can make your code tell Selenium to wait for the element to appear instead of polling for it or just hoping it will be there.

you will need these imports:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

remove the cookies:

driver.delete_all_cookies()

then fetch it like this

a = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, '//button[contains(text(),"Log in")]')))

you should also not call that variable a , use something more expressive like login_button

for more info see here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.