Home > Software design >  SyntaxError: Failed to execute 'evaluate' on 'Document' error using XPath via Se
SyntaxError: Failed to execute 'evaluate' on 'Document' error using XPath via Se

Time:07-27

I'm trying to make Spotify automatic song listener with selenium, but I'm getting an error while pressing the play button, please help.

The error I got:

File "c:\Users\angro\OneDrive\Masaüstü\Proje\main.py", line 38, in <module>
    driver.find_element(by=By.XPATH, value='//[@id="main"]/div/div[2]/div[3]/div[1]/div[2]/div[2]/div/div/div[2]/main/section/div[3]/div/div[1]/button').click()     
  File "C:\Users\angro\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 976, in find_element
    return self.execute(Command.FIND_ELEMENT, {
  File "C:\Users\angro\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "C:\Users\angro\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.InvalidSelectorException: Message: invalid selector: Unable to locate an element with the xpath expression //[@id="main"]/div/div[2]/div[3]/div[1]/div[2]/div[2]/div/div/div[2]/main/section/div[3]/div/div[1]/button because of the following error:
SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//[@id="main"]/div/div[2]/div[3]/div[1]/div[2]/div[2]/div/div/div[2]/main/section/div[3]/div/div[1]/button' is not a valid XPath expression.
  (Session info: chrome=103.0.5060.134)

Code:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException
from time import sleep 

driver = webdriver.Chrome("C:\chromedriver\chromedriver.exe")
driver.get("https://accounts.spotify.com/en/login")
login_button = driver.find_element_by_xpath('//*[@id="login-username"]')
login_button.click()
login_button.send_keys("mail")
password = driver.find_element_by_xpath('//*[@id="login-password"]')
password.click()
password.send_keys("password")
login_accept = driver.find_element_by_xpath('//*[@id="login-button"]/div[1]/p')
login_accept.click()
sleep(2)
gecis = driver.find_element_by_xpath('//*[@id="root"]/div/div[2]/div/div/button[2]/p')
gecis.click()
sleep(8)
search = driver.find_element_by_xpath('//*[@id="main"]/div/div[2]/nav/div[1]/ul/li[enter code here2]/a/span')
search.click()
sleep(2)
son2g_name = driver.find_element_by_xpath('//*[@id="main"]/div/div[2]/div[1]/header/div[3]/div/div/form/input')
son2g_name.send_keys("Nawy Midnight Dream")
sleep(5)
sonng = driver.find_element_by_xpath('//*[@id="searchPage"]/div/div/section[1]/div[2]/div/div/div/div[2]/a/div')
sonng.click()
sleep(5)
EC.element_to_be_clickable((By.XPATH, '//[@id="main"]/div/div[2]/div[3]/div[1]/div[2]/div[2]/div/div/div[2]/main/section/div[3]/div/div[1]/button'))
driver.find_element(by=By.XPATH, value='//[@id="main"]/div/div[2]/div[3]/div[1]/div[2]/div[2]/div/div/div[2]/main/section/div[3]/div/div[1]/button').click()
sleep(6)
driver.quit()

CodePudding user response:

you need check whether has a iframe, if yes, you need switch to iframe, then continue, please refer to selenium with iframe

CodePudding user response:

This error message...

SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//[@id="main"]/div/div[2]/div[3]/div[1]/div[2]/div[2]/div/div/div[2]/main/section/div[3]/div/div[1]/button' is not a valid XPath expression

...implies that the xpath expression you have constructed isn't a valid expression.

While constructing an xpath, the expression need to start with a tag_name e.g. <div>, <table> etc or * and always preceded by a double frontslash i.e. //


Solution

Effectively, your line of code will be:

driver.find_element(by=By.XPATH, value='//*[@id="main"]/div/div[2]/div[3]/div[1]/div[2]/div[2]/div/div/div[2]/main/section/div[3]/div/div[1]/button').click()
  • Related