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
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()
-
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– wmarchewka2015年12月01日 09:59:02 +00:00Commented 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– QAMember2015年12月01日 11:56:28 +00:00Commented 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– wmarchewka2015年12月01日 12:00:32 +00:00Commented Dec 1, 2015 at 12:00
Explore related questions
See similar questions with these tags.