0

I want to click on a button (event) using Selenium on Python and the button code is:

   <input id="workbenchLst:j_id_id509" name="workbenchLst:j_id_id509" onclick="A4J.AJAX.Submit('workbenchLst',event,{'similarityGroupingId':'workbenchLst:j_id_id509','parameters':{'ajaxSingle':'workbenchLst:j_id_id509','workbenchLst:j_id_id509':'workbenchLst:j_id_id509'} ,'containerId':'j_id_id1'} );return false;" value="Add" type="button" autocomplete="off">

My code:

driver.find_element_by_id("workbenchLst:j_id_id509").click()#add

and it isn't working, the error:

selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: [name="workbenchLst:j_id_id509"]
Steve
  • 55
  • 1
  • 5
  • @tuomastik While editing questions please don't add `` to the error trace logs. Doing so debugging becomes difficult as the _error messages_ in the _error stack trace_ gets **word wrapped**. – undetected Selenium Feb 04 '19 at 12:37

2 Answers2

1

Check for the iframe/frame in your page first, if there is a frame/iframe then you need to switch the frame first like below :

driver.switch_to_frame(driver.find_element_by_id("iframeid"));

You can try clicking on the element using below XPath:

element = driver.find_element_by_xpath("//input[contains(@id, 'workbenchLst') and @value='Add']");
element.click();

Or you can try using the JavaScript Executor like below :

element = driver.find_element_by_xpath("//input[contains(@id, 'workbenchLst') and @value='Add']");
driver.execute_script("arguments[0].click();", element);

Still not working then try to give some delay, import sleep from time like below :

from time import sleep
driver.switch_to_frame(driver.find_element_by_id("iframeid"));
sleep(5);
element = driver.find_element_by_xpath("(//input[contains(@id, 'workbenchLst') and @value='Add'])[2]");
element.click();

I hope it works...

Ali
  • 1,689
  • 1
  • 5
  • 12
  • Comments are not for extended discussion; this conversation has been [moved to chat](https://chat.stackoverflow.com/rooms/187871/discussion-on-answer-by-ali-cse-unable-to-locate-element-while-clicking-a-butto). – Samuel Liew Feb 04 '19 at 15:37
0

The desired element is a dynamic element so to locate the element you have to induce WebDriverWait for the element to be clickable and you can use either of the following solutions:

  • Using CSS_SELECTOR:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[id^='workbenchLst:'][name^='workbenchLst:'][value='Add']"))).click()
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[starts-with(@id,'workbenchLst:') and starts-with(@name,'workbenchLst:')][@value='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
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • getting this error on both Xpath and css_selector raise TimeoutException(message, screen, stacktrace) selenium.common.exceptions.TimeoutException: Message: – Steve Feb 04 '19 at 12:43
  • @Steve _TimeoutException_ is the outcome of **failed** _expected-conditions_. Debug your code through `find_element_by_*` in-conjunction with `time.sleep()`. If you are able to locate the element, update the question with the observations. – undetected Selenium Feb 04 '19 at 12:45
  • I didn't understand kindly note i'm a beginner. – Steve Feb 04 '19 at 12:49
  • Watch this discussion https://stackoverflow.com/questions/47993443/selenium-selenium-common-exceptions-nosuchelementexception-when-using-chrome/47995294#47995294 Check for presence of ` – undetected Selenium Feb 04 '19 at 12:51
  • It is inside divs but not iframes, still haven't figured it out. – Steve Feb 04 '19 at 14:01
  • @Steve Can you inspect the HTML and confirm if the _xpath_ or the _css_ identifies the desired element? – undetected Selenium Feb 04 '19 at 14:04
  • i still didn't understand how to do that, it would be great if you could edit your code to add that section – Steve Feb 04 '19 at 14:19