Home > database >  selenium.common.exceptions.InvalidSelectorException: Message: Given xpath expression is invalid usin
selenium.common.exceptions.InvalidSelectorException: Message: Given xpath expression is invalid usin

Time:08-19

I'm trying to develop an autologin für Instagram and I got the following problem.

Here is my code:

from time import sleep
from selenium import webdriver
from selenium.webdriver.common.by import By

browser = webdriver.Firefox()
browser.implicitly_wait(5)
browser.get('https://www.instagram.com/')
sleep(2)
login_link = browser.find_element(By.XPATH,"//button[text()=´Allow essential and optional cookies`]")

Here is the Error Message:

Traceback (most recent call last): File "C:\Users\justu\PycharmProject\botinsta\main.py", line 18, in login_link = browser.find_element(By.XPATH,"//button[text()=´Allow essential and optional cookies]") File "C:\Users\justu\PycharmProject\botinsta\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 857, in find_element return self.execute(Command.FIND_ELEMENT, { File "C:\Users\justu\PycharmProject\botinsta\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 435, in execute self.error_handler.check_response(response) File "C:\Users\justu\PycharmProject\botinsta\venv\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 247, in check_response raise exception_class(message, screen, stacktrace) selenium.common.exceptions.InvalidSelectorException: Message: Given xpath expression "//button[text()=´Allow essential and optional cookies]" is invalid: SyntaxError: Document.evaluate: The expression is not a legal expression Stacktrace: WebDriverError@chrome://remote/content/shared/webdriver/Errors.jsm:188:5 InvalidSelectorError@chrome://remote/content/shared/webdriver/Errors.jsm:348:5 find_@chrome://remote/content/marionette/element.js:320:11 element.find/</findElements<@chrome://remote/content/marionette/element.js:274:24 evalFn@chrome://remote/content/marionette/sync.js:136:7 PollPromise/<@chrome://remote/content/marionette/sync.js:156:5 PollPromise@chrome://remote/content/marionette/sync.js:127:10 element.find/<@chrome://remote/content/marionette/element.js:272:24 element.find@chrome://remote/content/marionette/element.js:271:10 findElement@chrome://remote/content/marionette/actors/MarionetteCommandsChild.jsm:245:25 receiveMessage@chrome://remote/content/marionette/actors/MarionetteCommandsChild.jsm:101:31

Can anyone help ?

CodePudding user response:

Try single quotes

'Allow essential and optional cookies'

instead of

´Allow essential and optional cookies`

P.S. Since you use browser.implicitly_wait(5) there is no need in time.sleep(2)

CodePudding user response:

You need to take care of a couple of things.

  • If you are passing the xpath within double quotes i.e. "..." then you need to provide the value of the attribues within single quotes i.e. '...'
  • <button> element is a clickable element. So you need to identify when the element is interactable.

Solution

Incorporating the above mentioned points and removing the sleep(2), your effective lines of code will be:

browser.get('https://www.instagram.com/')
login_link = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[text()='Allow essential and optional cookies']")))
  • Note: You have to add the following imports :

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