-1
 def conformation():
  try:
    conf_btn= driver.find_element(By.ID, "ext-gen1315")
    #Error_alert
    err_alert=driver.find_element_by_xpath("//a[@class='x-btn x-unselectable rp-btn-shadow rp-important-btn x-box-item x-toolbar-item x-btn-default-small x-noicon x-btn-noicon x-btn-default-small-noicon' and @id='button-1016']")
    err_alert.click()
    print("success")
    return 0   
  except NoSuchElementException:
    print ("conformation div element not found")
    return 1

In this function I need to check whether this ext-gen1315 div element is present, then click that err_alert button, else return 1.

Always I am getting NoSuchElementException only. While running this I could see this element in browser's inspect elements. I don't know where I made mistake.

raise exception_class(message, screen, stacktrace) selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//a[@class='x-btn x-unselectable rp-btn-shadow rp-important-btn x-box-item x-toolbar-item x-btn-default-small x-noicon x-btn-noicon x-btn-default-small-noicon' and @id='button-1016']"}

But i could see the element while inspecting enter image description here

Community
  • 1
  • 1
Divya Mani
  • 203
  • 1
  • 3
  • 15

4 Answers4

0

This should do:

conf_btn= driver.find_element(By.ID, "ext-gen1315")
if conf_btn:
    err_alert=driver.find_element_by_xpath("//a[@class='x-btn x-unselectable rp-btn-shadow rp-important-btn x-box-item x-toolbar-item x-btn-default-small x-noicon x-btn-noicon x-btn-default-small-noicon' and @id='button-1016']")
    err_alert.click()
    print("success")
    return 0 
else: 
    return 1
Juan C
  • 5,846
  • 2
  • 17
  • 51
0

Not sure what type of alert box this is but there is this driver method that is more elegant if its a standard alert. Probably wont work if this is a modal.

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException

try:
    WebDriverWait(browser, 3).until(EC.alert_is_present())
    driver.switch_to_alert.accept()
    return 0
except TimeoutException:
    return 1
ThePyGuy
  • 1,025
  • 1
  • 6
  • 15
0

I would rather use find_elements, so that you don't raise the unnecessary expections.

def conformation():
    if (len(driver.find_elements_by_xpath("//a[@class='x-btn x-unselectable rp-btn-shadow rp-important-btn x-box-item x-toolbar-item x-btn-default-small x-noicon x-btn-noicon x-btn-default-small-noicon' and @id='button-1016']"))>0):
        driver.find_element_by_xpath("//a[@class='x-btn x-unselectable rp-btn-shadow rp-important-btn x-box-item x-toolbar-item x-btn-default-small x-noicon x-btn-noicon x-btn-default-small-noicon' and @id='button-1016']").click()
        print("success")
        return 0
    else:
        print("conformation div element not found")
        return 1
supputuri
  • 13,644
  • 2
  • 21
  • 39
  • selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//a[@class='x-btn x-unselectable rp-btn-shadow rp-important-btn x-box-item x-toolbar-item x-btn-default-small x-noicon x-btn-noicon x-btn-default-small-noicon' and @id='button-1016']"} (Session info: chrome=80.0.3987.132) – Divya Mani Mar 10 '20 at 08:47
0

This error message...

NoSuchElementException

...implies that the WebDriver instance was unable to locate the desired element.


Ext JS

Ext JS is a JavaScript framework for building data-intensive, cross-platform web and mobile applications for any modern device.


This usecase

There are two issues with the line of code:

conf_btn= driver.find_element(By.ID, "ext-gen1315")
  • First of all as the desired element is a JavaScript enabled element so to locate the element you need to induce WebDriverWait for the visibility_of_element_located().
  • Next, the suffix in the value of the id attribute changes i.e. dynamic in nature you won't be able to use the complete value of the id attribute.

Solution

As the value of the id attribute is dynamic in nature you can use only partial value which is static. As an example, for the following HTML:

<div id="ext-gen1315" class="bats-table"></div>

To identify the <div> node you need to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR:

    conf_btn = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div[id^='ext-gen']")))
    
  • Using XPATH:

    conf_btn = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, "//div[starts-with(@id,'ext-gen')]")))
    
  • 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
    

However, there would be a lot other elements with id attribute starting with ext-gen. So to uniquely identify the <table> element you need to club up the class attribute as follows:

  • Using CSS_SELECTOR:

    WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.bats-table[id^='ext-gen']")))
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[@class='bats-table' and starts-with(@id,'ext-gen')]")))
    

Reference

You can find a relevant detailed discussion in:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • i am getting no such element exception selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//a[@class='x-btn x-unselectable rp-btn-shadow rp-important-btn x-box-item x-toolbar-item x-btn-default-small x-noicon x-btn-noicon x-btn-default-small-noicon' and @id='button-1016']"} – Divya Mani Mar 10 '20 at 08:39