1

I'm writing a program to automate some actions on a website using Python and Selenium and I need to click a button with this code:

<button class="button login" data-popup="login">Change User</button>

Using this

log_but2 = "//button[@class='button login']"
driver.find_element_by_xpath(log_but2).click()

I've tried many combinations of Xpath but neither of them can locate the button, in addition I don't get any error, simply the program continue like it has clicked on it and get another error. Someone can help me please?

1 Answer 1

0

you can't use the compound class in xpath.

Try below code

log_but2 = "//button[@data-popup='login']"
driver.find_element_by_xpath(log_but2).click()
3
  • You can use it by contains() function. For example xpath in this case could look like this: "//button[contains(@class, 'button login')]". But this is risky because this expression will find all buttons that contains in class attribute 'button login'. The same situation is with @QAMember hint. Solution for that could be to make xpath that will look for attribute and text in button, for example: "//button[@data-popup='login' and text()='Change User']" or with contains(): "//button[contains(@class, 'button login') and text()='Change User']"
    – wmarchewka
    Dec 1, 2015 at 9:59
  • @wmarchewka, your suggested solution also work fine.But attribute "data-popup" was given by developers for their back end purpose.So I don't think they will give duplicates in same page with same values.
    – QAMember
    Dec 1, 2015 at 11:56
  • trust me, if this is big Web application, sooner or later there will appear duplicates. I know this from my experience. If You create XPaths that should stay for a long time it's better to make them unique from the begging ;)
    – wmarchewka
    Dec 1, 2015 at 12:00

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.