I have a Website were I want to click a button, but I can't find in the button in the HTML script, what I have to write in my Python script? Later I want to have something like


Now I don't know what I have to put in there

<div class="custom-col" onclick="Ajax('routes.php','main');mainMenuLoad('mainMenuRoutes');" style="width: 20%;">
                        <img src="assets/img/icons/route-icon-shade.png" class="btn-action" id="mainMenuRoutes" style="height:40px;">

I hope you understood my question because I am not a native speaker

  • is there any issue with using id of <img> tag ?
    – NarendraR
    Commented Sep 10, 2018 at 9:26

3 Answers 3

<img src="assets/img/icons/route-icon-shade.png" class="btn-action" id="mainMenuRoutes" style="height:40px;">

For this element, CSS Selector will be:

.btn-action // A dot followed by the class of the element


#mainMenuRoutes // A hash followed by the id of the element

Read this for learning more about CSS Selectors.

  • Thanks for the fast answer, but now I don know why he can't find the button. My command is driver.find_element_by_css_selector(".btn-action").click() and now i get this error selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".btn-action"} I Hop u can help me one more time And also the driver.find_element_by_css_selector("'mainMenuRoutes").click() command doesnt work for me
    – Laurenz Z
    Commented Sep 10, 2018 at 19:55
  • Add wait before clicking the element. Otherwise, have a try with div instead of img.
    – Shahid
    Commented Sep 11, 2018 at 3:08

Not sure what you mean by not being able to find the button in the HTML code. It looks like this is the button you are trying to catch:

<img src="assets/img/icons/route-icon-shade.png" class="btn-action" id="mainMenuRoutes" style="height:40px;">

If this is indeed the button, then you are very lucky, because it has an id! Which is a unique identifier, so you can be sure that your script is going to click the specific button.

Then the css selector would be id="mainMenuRoutes" or css=#mainMenuRoutes. You can read under the The id Selector in here.


As per the given HTML you can use below mentioned locators that we usually use in automation testing company.

By CSS Selector:

return driver.find_element_by_css_selector(‘#mainMenuRoutes’).click()
return driver.find_element_by_css_selector(‘.btn-action’).click()

By Class Name:


By Id:


By Xpath:


You can use either of the above mentioned locator to locate the element and click.

Hope issue will be resolved. Further, if you still face any issue you can provide webdriver wait for element before the click function. Refer to this Webdriver Wait


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.