0

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

driver.find_element_by_css_selector()

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;">
                    </div>

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

  • is there any issue with using id of <img> tag ? – NarendraR Sep 10 '18 at 9:26
0
<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

or,

#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 Sep 10 '18 at 19:55
  • Add wait before clicking the element. Otherwise, have a try with div instead of img. – Shahid Sep 11 '18 at 3:08
0

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.

0

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()
OR
return driver.find_element_by_css_selector(‘.btn-action’).click()

By Class Name:

driver.find_element_by_class_name(‘custom-col’).click()

By Id:

driver.find_element_by_id(‘mainMenuRoute’).click()

By Xpath:

driver.find_element_by_Xpath(“//img[@src='assets/img/icons/route-icon-shade.png']").click();
or
driver.find_element_by_Xpath("//div[@class=‘customcol’]//div[@id=‘mainMenuRoutes’]").click()

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, privacy policy and cookie policy

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