Search code examples
seleniumselenium-webdriverxpathpython-3.7xpath-1.0

selenium.common.exceptions.InvalidSelectorException using Selenium in python 3.7


I want to use selenium to automate the process from open a specific website to log in to search for particular articles. Few of the steps I could do it but facing error in 'sign in' step.

from selenium import webdriver
from selenium.webdriver.common.by import By
base = 'https://www.wsj.com'
url = 'https://www.wsj.com/search/term.html?KEYWORDS=cybersecurity&min-date=2018/04/01&max-date=2019/03/31&isAdvanced=true&daysback=90d&andor=AND&sort=date-desc&source=wsjarticle,wsjpro&page=1'

browser = webdriver.Safari(executable_path='/usr/bin/safaridriver')
browser.get(url)
browser.find_element_by_id('editions-select').click()
browser.find_element_by_id('na,us').click()
browser.find_element(By.XPATH, '//button[@type="button"],[contain(.,"Sign In")]').click()
browser.find_element_by_id('username').send_keys('**#&^&@$@#$')
browser.find_element_by_id('password').send_keys('@#@$%%**')
browser.find_element_by_id('basic-login').click()
browser.find_element_by_id('masthead-container').click()
browser.find_element_by_id('searchInput').send_keys('cybersecurity')
browser.find_element_by_name('ADVANCED SEARCH').click()
browser.find_element_by_id('dp1560924131783').send_keys('2018/04/01')
browser.find_element_by_id('dp1560924131784').send_keys('2019/03/31')
browser.find_element_by_id('wsjblogs').click()
browser.find_element_by_id('wsjvideo').click()
browser.find_element_by_id('interactivemedia').click()
browser.find_element_by_id('sitesearch').click()

The code is working till this line:

browser.find_element_by_id('na,us').click()

But after that it is showing error in this line:

browser.find_element(By.XPATH, '//button[@type="button"],[contain(.,"Sign In")]').click()

The error message says​:

selenium.common.exceptions.InvalidSelectorException: Message: 

What is wrong is my code?


Solution

  • This error message...

    selenium.common.exceptions.InvalidSelectorException
    

    ...implies that the XPath expression was not a valid one.

    However, it seems you were close. You need to replace:

    '//button[@type="button"],[contain(.,"Sign In")]'
    

    and join the two condition with and operator as follows:

    "//button[@type='button' and contains(.,'Sign In')]"