Python使用Selenium根据类型,值和类单击按钮

时间:2014-12-30 15:14:26

标签: python selenium

这就是html的样子:

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

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

这就是我正在尝试的:

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

无论出于何种原因,这实际上并没有点击按钮,它只是向下移动到按钮所在的位置,它只是在那里等待。所以也许在某个地方还有另一个隐藏的按钮,我无法看到代码是&#34;点击&#34;上。不确定,所以我想我的问题真的归结为,有没有办法可以根据它的价值,类型和类来搜索按钮?

2 个答案:

答案 0 :(得分:5)

基于text的搜索。这是我最喜欢的

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

使用.我们直接指向html层次结构中的父级,*允许您在不依赖任何特定tag的情况下进行搜索

答案 1 :(得分:0)

解决方案可能是:

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

有关Selenium Action Chain的文档:http://selenium-python.readthedocs.org/en/latest/api.html#module-selenium.webdriver.common.action_chains