0

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>
cruisepandey
  • 28,520
  • 6
  • 20
  • 38
codeeasy
  • 1
  • 3
  • Could you share how you reach to that website, which website you are trying to parse or the whole HTML structure of the page? – Muhteva Sep 06 '21 at 08:17
  • Hi @Muhteva, the webpage I'm using a company proprietry. So couldn't share that. But basically, this is a timesheet webpage. – codeeasy Sep 06 '21 at 08:20
  • XPATH you are using seems correct to me. It would be helpful if you could at least share more information about the HTML structure. Where is this ```span``` exactly? – Muhteva Sep 06 '21 at 08:23
  • '' – codeeasy Sep 06 '21 at 08:28
  • This is another way to reach to that element: ```driver.find_element_by_css_selector("span.sapMSltArrow").click()``` Maybe this could work, don't know. You mentioned that it's a dropdown element. Make sure that it is visible in the webpage. Maybe you have to click on something before trying to locate it. – Muhteva Sep 06 '21 at 08:32
  • You can use direct text to click on it, `//label[text()='Fiscal Year']` code would be : `WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//label[text()='Fiscal Year']"))).click()` – cruisepandey Sep 06 '21 at 09:03

1 Answers1

1

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()
cruisepandey
  • 28,520
  • 6
  • 20
  • 38