0

I used selenium IDE to trace my UI activity. I got this following code from IDE and i inspected in UI also,but while using find_element by id i'm getting css selector error.

driver.find_element(By.ID, "button-1034-btnIconEl").click()

error is

raise exception_class(message, screen, stacktrace) NoSuchElementException: no such element: Unable to locate element: {"method":"css selector","selector":"[id="button-1034-btnIconEl"]"}
(Session info: chrome=78.0.3904.108)

enter image description here

Please help me to debug this..

Guy
  • 34,831
  • 9
  • 31
  • 66
Anu Priya
  • 13
  • 1
  • 6

2 Answers2

0

The id seems to be dynamic one so you cannot use static id in the selector. You need to use a dynamic xpath for this.
You can use the below xpath:

driver.find_element(By.XPATH, "//span[contains(@id,'btnIconEl')]").click()

OR

You can find the element using its text as well in the xpath:

driver.find_element(By.XPATH, "//span[contains(text(),'Add Order')]").click()
Sameer Arora
  • 4,161
  • 2
  • 6
  • 20
  • Hi Sameer , i tried first one getting, "ElementNotInteractableException: element not interactable " Error. and if i use second, same error "no such element" – Anu Priya Dec 12 '19 at 09:09
  • yes i can.i used selenium IDE chrome extension, through that only i got this code. – Anu Priya Dec 12 '19 at 09:33
  • @AnuPriya i have edited the second xpath, please try that one and let me know the result – Sameer Arora Dec 12 '19 at 09:35
  • Finally what i found was inside div there is one iframe so in that iframe this button is presented. so now how to select the frame? – Anu Priya Dec 12 '19 at 10:36
  • Yes so you need to first switch to the iframe and then click on the button. To switch on the iframe you should use: `driver.switch_to.frame(driver.find_element_by_id('iframe'))` and then you need to click on the button using the xpath mentioned in my answer – Sameer Arora Dec 12 '19 at 10:39
  • @AnuPriya if the iframe id is not present then please send me the html of the iframe, i will help you to detect the iframe using any other locator – Sameer Arora Dec 12 '19 at 10:41
  • Thanku @Sameer. for the confidentiality of my company i can't share but i posted the snippet please comment in this link https://stackoverflow.com/questions/59302960/how-to-click-button-which-is-inside-iframe-using-python-selenium – Anu Priya Dec 12 '19 at 10:55
  • @AnuPriya I am sure that you discovered the _iframe_ after going through my answer ;) you are denying my doughnut :) – DebanjanB Dec 12 '19 at 12:26
0

To click() on the button with text as Add Order you have to induce WebDriverWait for the element_to_be_clickable() you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[id^='button-'] > span.x-btn-wrap > span.x-btn-button > span.x-btn-inner.x-btn-inner-center"))).click()
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[starts-with(@id, 'button-')]/span[@class='x-btn-wrap']/span[@class='x-btn-button']/span[@class='x-btn-inner x-btn-inner-center' and contains(., 'Add')]"))).click()
    
  • 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
    

Reference

You can find a detailed discussion on NoSuchElementException: no such element in Selenium “selenium.common.exceptions.NoSuchElementException” when using Chrome

DebanjanB
  • 118,661
  • 30
  • 168
  • 217