0

So this is what the html looks like:

<button type="submit" name="page" value="2" class="_btn _btng">Next →</button>

<button type="submit" name="page" value="1" class="_btn">← Back</button>

And this is what I'm attempting:

driver.find_element_by_xpath("//*[contains(text(), 'Next')]").click()

For whatever reasons, this isn't actually clicking the button, it's just moving down to where the button is located and it just waits there. So maybe there's another hidden button somewhere that I can't visibly see that the code is "clicking" on. Not sure, so I guess my question really comes down to, is there a way where I can search for a button based on its value, type, and class?

2 Answers 2

5

Give the text based search a shot. That's my favourite

driver.find_element_by_xpath("//*[.='Next →')]").click()

With . we are directly pointing to parent in html hierarchy and * allows you to do a search without depending on any specific tag

Sign up to request clarification or add additional context in comments.

7 Comments

I tried doing that, but the → character doesn't seem to be understood... :(
In that case go for //button[contains(text(), 'Next')]
Have you tried cssSelector [value='2'][class='_btn _btng']
I have not tried using cssSelector, in the past it never seemed to work for me, but I'll give it a spin.
As I said with * you are doing a tag independent search. You might have some other tag say for example a with same text that's why it was failing. Now with //button[....] you are just searching button tags eliminating others.
|
0

A solution could be:

buttons = self.driver.find_elements_by_xpath("//title[contains(text(),'Next')]")
actions = ActionChains(self.driver)
time.sleep(2)
actions.click(button)
actions.perform()

Documentation on Selenium Action Chain here: http://selenium-python.readthedocs.org/en/latest/api.html#module-selenium.webdriver.common.action_chains

2 Comments

Could you explain exactly what that's doing?
find the button with "next" text , create an action chain, press the button. There is even a delay by using the time sleep in case there is code loaded by ajax

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.