codeeasy
codeeasy

Reputation: 1

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: - drop down item ID,XPATH did not work

Could someone help me in cracking this down?

This is the element part in website: <span id="__xmlview0--__idDateType-arrow" class="sapMSltArrow"></span>

This is my find element part in python script:

driver.find_element_by_xpath("//*[@id='__xmlview0--__idDateType-arrow']").click()

Error message:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id='__xmlview0--__idDateType-arrow']"} (Session info: chrome=92.0.4515.159)

Edit :

<div id="_xmlview0--__idDateType" data-sap-ui="__xmlview0--__idDateType" style="width:100%;max-width:100%" class="sapMSlt sapMSltDefault sapMSltHoverable sapMSltWithArrow" aria-required="true" aria-labelledby="__xmlview0--filbar-filterItem-___INTERNAL-MyOwnFilterField __xmlview0--__idDateType-label" role="combobox" aria-expanded="false" aria-live="polite" tabindex="0"><label id="__xmlview0--__idDateType-label" for="__xmlview0--__idDateType" class="sapMSltLabel">Fiscal Year</label><span id="__xmlview0--__idDateType-arrow" class="sapMSltArrow"></span></div>

Upvotes: 0

Views: 3025

Answers (1)

cruisepandey
cruisepandey

Reputation: 29362

This exception

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

Implies either one of them, element locator is not correct, element is not rendered properly, element is in iframe.

Detail illustration below

  1. Element locator is not correct, or Element is dynamic in nature i.e. element locator id, class_name are getting generated by server by using javascript frameworks.

Solutions

try to use a reliable locator. which may be having a stable preceding-sibling or following-sibling or ancestor or descendant node. And based on the mentioned node, try to use xpath to reach to the desired element.

Always check in HTML DOM, if we have unique entry or not for the element we wanna interact with.

Steps to checks :-

Press F12 in Chrome -> go to element section -> do a CTRL + F -> then paste the locator and see, if the desired element is getting highlighted or not, if it getting highlighted, Do we have unique entry 1/1 or not ?

  1. Element is not rendered completely/partial loaded, and you are trying to interact with it.

Solution

Basically, a sleep will help us here. You can try to put time.sleep(5) here to just debug and if that works, then later it can be implemented via Explicit waits.

Example :-

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, ""))).click()

Imports :-

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
  1. Element is in iframe/frame/frameset

Solutions

Check for any iframe that could be a parent/ancestor node to the node we want to interact with. if it happens to be, you need to switch the driver focus to iframe first and then can interact with the desired element.

Code to switch to iframe :

WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, "")))

after switch to the iframe, you can interact with the either driver.find_element or Explicit waits as mentioned above.

Actual problem to this specific ticket :

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//label[text()='Fiscal Year']"))).click()

Upvotes: 2

Related Questions