Selenium.Common.Exceptions.Nosuchelementexception: Message: No Such Element: Unable to Locate Element While Trying to Click Next Button With Selenium

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element while trying to click Next button with selenium

This error message...

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"name","selector":"submitNext"}
(Session info: chrome=66.0.3359.170)
(Driver info: chromedriver=2.36.540470)

...implies that the ChromeDriver was unable to locate the desired element.

Locating the desired element

As per the HTML you have shared to click on the element you can use either of the following Locator Strategies :

  • css_selector :

    driver.find_element_by_css_selector("input[name='submitNext'][value='Next']").click()
  • xpath :

    driver.find_element_by_xpath("//input[@name='submitNext' and @value='Next']").click()

However your main issue is the version compatibility between the binaries you are using as follows :

  • You are using chromedriver=2.36
  • Release Notes of chromedriver=2.36 clearly mentions the following :

Supports Chrome v63-65

  • You are using chrome=66.0
  • Release Notes of ChromeDriver v2.38 clearly mentions the following :

Supports Chrome v65-67

  • Your Selenium Client version is unknown to us.

So there is a clear mismatch between the ChromeDriver v2.36 and the Chrome Browser v66.0

Solution

  • Upgrade Selenium to current levels Version 3.11.0.
  • Upgrade ChromeDriver to current ChromeDriver v2.38 level.
  • Keep Chrome version at Chrome v66.x levels. (as per ChromeDriver v2.38 release notes)
  • Clean your Project Workspace through your IDE and Rebuild your project with required dependencies only.
  • Use CCleaner tool to wipe off all the OS chores before and after the execution of your test Suite.
  • If your base Web Client version is too old, then uninstall it through Revo Uninstaller and install a recent GA and released version of Web Client.
  • Take a System Reboot.
  • Execute your @Test.

NoSuchElementException when using Selenium

Aan <inpt> element is a clickable element and ideally to locate any clickable 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:

    element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#nickname[name='nickname'][placeholder='Nickname'][data-functional-selector='username-input']")))
  • Using XPATH:

    element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@id='nickname' and @name='nickname'][@placeholder='Nickname' and @data-functional-selector='username-input']")))
  • 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 discussions on NoSuchElementException in:

  • selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element while trying to click Next button with selenium
  • selenium in python : NoSuchElementException: Message: no such element: Unable to locate element

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {method:css selector,selector:.ui flu~}

As per the HTML:

HTML

To identify the clickable element with text as Create download package you can use either of the following Locator Strategies:

  • Using xpath:

    b = dr.find_element(By.XPATH, "//button[@class='ui fluid right labeled icon primary button' and contains(., 'Create download package')]")

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 XPATH:

    b = WebDriverWait(dr, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@class='ui fluid right labeled icon primary button' and contains(., 'Create download package')]")))
  • 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

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element (XPath)

After clicking on view All, you have not mentioned what exactly you wanna do, if I assume that you wanna fetch restaurant name, you could do that by the below method :

you can write below code after these two lines from your code : -

view_all.click()
time.sleep(10)

Sample code :

all_names = driver.find_elements(By.CSS_SELECTOR, "div[role='heading'] div")
print(all_names[0].text)

or in case you would like to fetch all the names :-

names = []
for name in driver.find_elements(By.CSS_SELECTOR, "div[role='heading'] div"):
names.append(name.text)


print(names)

Updated 1 :

driver = webdriver.Chrome("<path to chromedriver.exe>")
driver.maximize_window()
driver.implicitly_wait(30)
location = "545084"
query = "food near " + location
driver.get("https://www.google.com/search?q=" + query)
wait = WebDriverWait(driver, 10)
ActionChains(driver).move_to_element(wait.until(EC.element_to_be_clickable((By.XPATH, "//a[contains(@href, '/search?tbs')]")))).perform()
wait.until(EC.element_to_be_clickable((By.XPATH, "//a[contains(@href, '/search?tbs')]"))).click()
names = []
for name in driver.find_elements(By.XPATH, "//div[@aria-level='3']"):
names.append(name.text)

print(names)

Output :

["Mum's Kitchen\nCatering Pte Ltd", 'Kitch', "Zoey's Diner", "McDonald's", 'ThaiExpress', 'Dian Xiao Er', "Swensen's", 'Pho Street Compass One', 'Singapore WaterDrop Tea House', 'LeNu at Compass One', "McDonald's Compass One", "McDonald's", 'Miami Bistro', 'Monster Curry', 'Texas Chicken (Hougang Capeview)', 'Paradise Hotpot at Compass One', "Long John Silver's Rivervale Mall", 'Boon Tong Kee @ Compass One', 'Din Tai Fung', 'Fish & Co.', 'PUTIEN', 'Soup Restaurant 三盅两件 - Compass One']

How to identify this element using selenium and Python?

To click on the element with text as Data you can use either of the following locator strategies:

  • Using link_text:

    driver.find_element(By.LINK_TEXT, "Data").click()
  • Using css_selector:

    driver.find_element(By.CSS_SELECTOR, "a.ui-tabs-anchor[id^='ui-id'][title]").click()
  • Using xpath:

    driver.find_element(By.XPATH, "//a[@class='ui-tabs-anchor' and starts-with(@id, 'ui-id')][text()='Data']").click()

The desired element is a dynamic element, so ideally to click() on the clickable element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:

  • Using LINK_TEXT:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.LINK_TEXT, "Data"))).click()
  • Using CSS_SELECTOR:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.ui-tabs-anchor[id^='ui-id'][title]"))).click()
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@class='ui-tabs-anchor' and starts-with(@id, 'ui-id')][text()='Data']"))).click()
  • 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 discussions on NoSuchElementException in:

  • selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element while trying to click Next button with selenium
  • selenium in python : NoSuchElementException: Message: no such element: Unable to locate element


Related Topics



Leave a reply



Submit