Stupid.Fat.Cat
Stupid.Fat.Cat

Reputation: 11285

Python using Selenium to click on a button based on type, value, and class

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?

Upvotes: 0

Views: 3189

Answers (2)

aberna
aberna

Reputation: 5814

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

Upvotes: 0

Saifur
Saifur

Reputation: 16201

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

Upvotes: 5

Related Questions