Selenium in Python: Nosuchelementexception: Message: No Such Element: Unable to Locate Element

selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element:

Could be a race condition where the find element is executing before it is present on the page. Take a look at the wait timeout documentation. Here is an example from the docs

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

driver = webdriver.Firefox()
driver.get("http://somedomain/url_that_delays_loading")
try:
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, "myDynamicElement"))
)
finally:
driver.quit()

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 (Python) - NoSuchElementException: Message: no such element: Unable to locate element

To expand the Scotch Whisky menu you don't need to click on the item, instead you can simply Mouse Hover and grab a screenshot of the webpage using either of the following Locator Strategies:

  • Using CSS_SELECTOR:

    driver.get('https://www.thewhiskyexchange.com/')
    ActionChains(driver).move_to_element(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "a[title='Scotch Whisky']")))).perform()
    driver.save_screenshot('./Scotch_Whisky.png')
    driver.quit()
  • Using XPATH:

    driver.get('https://www.thewhiskyexchange.com/')
    ActionChains(driver).move_to_element(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//a[@title='Scotch Whisky']")))).perform()
    driver.save_screenshot('./Scotch_Whisky.png')
    driver.quit()
  • Note : You have to add the following imports :

    from selenium.webdriver.common.action_chains import ActionChains
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
  • Screenshot:

Scotch_Whisky

selenium in python : NoSuchElementException: Message: no such element: Unable to locate element

The username and password fields are within an frame, so you have to:

  • Induce WebDriverWait for the desired frame to be available and switch to it.
  • Induce WebDriverWait for the desired element to be clickable.
  • You can use the following solution:

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

    driver = webdriver.Firefox(executable_path=r'C:\\Utility\\BrowserDrivers\\geckodriver.exe')
    driver.get("http://sugang.korea.ac.kr")
    WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.NAME,"firstF")))
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.input_login[name='id']"))).send_keys('abc')
    driver.find_element_by_css_selector("input.input_login[name='pw']").send_keys("cdef")
  • Browser Snapshot:

sugang_korea_ac_kr



Related Topics



Leave a reply



Submit