Search code examples
seleniumxpathpython-3.7

selenium.common.exceptions.NoSuchElementException using selenium in python 3.7


I am trying 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' and contains(.,'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' and contains(.,'Sign In')]").click()

The error message says:

selenium.common.exceptions.NoSuchElementException

What should I do to make it work? What is the problem?


Solution

  • You should use WebDriverWait:

    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    # from selenium.webdriver.support.wait import WebDriverWait
    from selenium.webdriver.support.ui import WebDriverWait
    
    botton_to_click = WebDriverWait(driver, 10).until(EC.element_to_be_clickable, ((By.XPATH,"//button[@type='button' and contains(.,'Sign In')]")))
    botton_to_click.click()
    

    Hope this helps you!