简体   繁体   中英

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

I am trying to identify a button in a 3 Step - dialog, so same 'Next' button need to be clicked in first 2 steps of dialog, When used same xpath for first step, which works fine, but failing in step 2.

My Python Code:

a=driver.find_element_by_xpath(".//*[@id='create-portal-popup']/div[4]/div[1]/button[3]")
a.send_keys(selenium.webdriver.common.keys.Keys.SPACE)

The above code works and element is clicked.

driver.implicitly_wait(30)
b=driver.find_element_by_xpath(".//*[@id='create-portal-popup']/div[4]/div[1]/button[3]")
b.send_keys(selenium.webdriver.common.keys.Keys.SPACE)

This fails with below exception.

Exception Observed:

Traceback (most recent call last):
  File "C:\Users\sabarish.kannan\workspace1\de.vogella.python.first\src\FirstModule.py", line 49, in <module>
    b=driver.find_element_by_xpath(".//*[@id='create-portal-popup']/div[4]/div[1]/button[3]")
  File "C:\Users\sabarish.kannan\AppData\Local\Programs\Python\Python36\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 354, in find_element_by_xpath
    return self.find_element(by=By.XPATH, value=xpath)
  File "C:\Users\sabarish.kannan\AppData\Local\Programs\Python\Python36\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 832, in find_element
    'value': value})['value']
  File "C:\Users\sabarish.kannan\AppData\Local\Programs\Python\Python36\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 297, in execute
    self.error_handler.check_response(response)
  File "C:\Users\sabarish.kannan\AppData\Local\Programs\Python\Python36\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 194, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":".//*[@id='create-portal-popup']/div[4]/div[1]/button[3]"}
  (Session info: chrome=61.0.3163.100)
  (Driver info: chromedriver=2.32.498550 (9dec58e66c31bcc53a9ce3c7226f0c1c5810906a),platform=Windows NT 10.0.10240 x86_64)

If you are not able to locate the element,then definitely there is some change with your DOM making your xpath invalid after you navigated to step-2.

  1. Be sure of element you are trying to locate,dont just assume that both 'Next' button are same in step-1 and step-2.
  2. Try using different locator method maybe using id/class/css/other selector methods,if you can find associated with 'Next' button in step-2.

Cheers! :-)

We need to consider a few things here. As you mentioned "When used same xpath for first step, which works fine, but failing in step 2" is pretty correct. 3 separate elements can't have the same xpath at the same time on the HTML DOM .

  1. Your first click() works because the xpath which is not unique matched the first Next button. I would suggest you to reconstruct the xpath of the first Next button to make it unique
  2. A button is always expected to have an onClick() event which will change the HTML DOM . So when you invoke click() method, the HTML DOM changes. The reason is as stated above ie change in the HTML DOM due to presence of Javascript or Ajax Calls . So after the first click() is invoked we must try to construct a unique xpath for the second Next button again.
  3. Similarly, for the reasons stated above you need to construct a unique xpath for the third Next button too.

Taking care of all these fact you will see away the error :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM