1

Im trying to get a HTML element from twitter.com search bar. Using selenium I'm using the find_element_by_xpath method.

Code

search_input = driver.find_element_by_xpath('//input[@aria-label="Search query"]')

Error

Traceback (most recent call last):
  File "C:\Users\jorda\OneDrive - Limerick Institute Of Technology\College\Semester 6\Data Analytics\Data Scraping\twit'ter.py", line 25, in <module>
    search_input = driver.find_element_by_xpath('//input[@data-testid="SearchBox_Search_Input""]')
  File "C:\Users\jorda\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 394, in find_element_by_xpath
    return self.find_element(by=By.XPATH, value=xpath)
  File "C:\Users\jorda\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 978, in find_element
    'value': value})['value']
  File "C:\Users\jorda\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "C:\Users\jorda\AppData\Local\Programs\Python\Python37\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 //input[@data-testid="SearchBox_Search_Input""] because of the following error:
SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//input[@data-testid="SearchBox_Search_Input""]' is not a valid XPath expression.
  (Session info: chrome=86.0.4240.198)

The element I'm trying to get is supposed to be aria-label="Search query". Any help is appreciated.

10
  • If you read the error, it says : "is not a valid XPath expression." Commented Nov 16, 2020 at 4:33
  • seei im just following a video and the code is the exact same. I don't see what the error is Commented Nov 16, 2020 at 4:34
  • send me the link of site that you want to find xpath in it Commented Nov 16, 2020 at 4:36
  • twitter.com/home Commented Nov 16, 2020 at 4:37
  • Im following this video : youtube.com/… about 5:50 in Commented Nov 16, 2020 at 4:37

1 Answer 1

1

This error message...

selenium.common.exceptions.InvalidSelectorException: Message: invalid selector: Unable to locate an element with the xpath expression //input[@data-testid="SearchBox_Search_Input""] because of the following error:
SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//input[@data-testid="SearchBox_Search_Input""]' is not a valid XPath expression.

...implies that the xpath which you have used wasn't a valid xpath expression.

The error was observed for the xpath as:

//input[@data-testid="SearchBox_Search_Input""]

is different from the xpath you have provided as code trials.

However the with in your code trial is a valid xpath and won't raise this error.


Ideally, to click on the element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR:

    search_input = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[aria-label='Search query']")))
    
  • Using XPATH:

    search_input = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@aria-label='Search query']")))
    
  • 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
    

References

You can find a couple of relevant detailed discussions in:

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.