0

Hello I am trying to use selenium to find a button to click on. Below is a snippet of the HTML code i am working with.

<input type="button" id="runButton" class="button" value="Run Report" onclick="chooseRun()">

I am trying to click on the runButton with the code below.

elem = driver.find_element_by_id('runButton').click()

I am getting the following error message:

NoSuchElementException: Message: Unable to find element with css selector == [id="runButton"]

Not sure what else to try.

enter image description here

  • Please attach the screenshot of the inspect element page and also the code you have tried so far. SO is for helping developers find solutions to their problem. It's mainly debugging. It's really difficult to help you if don't provide the code :) – Debdut Goswami Jan 10 '20 at 22:43
  • I posted the HTML of the inspect element as well as he code i tried. I went ahead and attached a picture of the inspect element. – user10297084 Jan 10 '20 at 22:53

2 Answers2

0

Most likely what you'll need to do to find your element is to use waits. You need to allow time for an element to be visible, clickable, etc. before you can interact with it. You can find information on waits here: https://selenium-python.readthedocs.io/waits.html

Taken from the above website:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

elem = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "runButton"))

If waits do not work then it's possible that your element is inside an iframe. You will need to switch to that iframe first and then search for the element in order to find it.

You will find the iframe like you would another element and then switch to it like this:

iframe = driver.find_element_by_id("content_Iframe")
driver.switch_to.frame(iframe)

button = driver.find_element_by_id("runButton")
button.click()

Once you're done with the iframe and it's contents, you will need to switch back out of it:

driver.switch_to.default_content()
RKelley
  • 1,069
  • 7
  • 14
  • Thank you for the reply RKelley. I tried that and even put the wait time to 30 and it didnt work. I cant even find a parent element but its ID as well. Could it be that nothing is visible or a bug in selenium? – user10297084 Jan 10 '20 at 22:50
  • If you've added waits and it still doesn't find the element then it could be inside an iFrame. Can you add the page's html to your question? – RKelley Jan 10 '20 at 22:51
  • I guess my problem is i cant find anything on this page. I tried finding an element using the element by Id method but it cant find the Id. The button seems to reside in a table. I have posted a screen shot of the HTML above. – user10297084 Jan 14 '20 at 01:19
  • You should still be able to find it even if it's in a table. Can you give all the html? If your button is inside an iframe, you won't be able to detect until to switch to that iframe first. – RKelley Jan 14 '20 at 01:33
  • I was able to determine this table is inside an iframe. Please see Iframe below. I am unable to post the entire HTML code. I was able to find this iframe by its ID. What do i need to do next? – user10297084 Jan 14 '20 at 19:42
  • I've edited my answer to use the iframe id to locate it, switch to it and then to find and click the button. – RKelley Jan 14 '20 at 19:58
0

The element seems to be a dynamic element so to click() on the element you need to use element_to_be_clickable() and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.button#runButton[value='Run Report']"))).click()
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@class='button' and @id='runButton'][@value='Run Report']"))).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
    
DebanjanB
  • 118,661
  • 30
  • 168
  • 217