在webapps中找到按钮的Xpath [Selenium]

时间:2016-03-21 20:56:32

标签: python selenium-webdriver

我使用selenium浏览此页面:

https://webapps.cityofchicago.org/activegcWeb/

但是我无法找到如何移动到任何页面,使用chrome我会得到这个Xpath用于' next'按钮:

'//*[@id="id2"]/a[3]'

我正在使用此代码:

url = 'https://webapps.cityofchicago.org/activegcWeb/'
driver_1 = webdriver.Firefox()
driver_1.get(url)
content = driver_1.page_source

next_button_xpath = '//*[@id="id2"]/a[3]' 
button = driver_1.find_element_by_xpath(next_button_xpath)
button.click()

但我收到了这个错误:

'selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: {"method":"xpath","selector":"//*[@id=\"id2\"]/a[3]"}'

2 个答案:

答案 0 :(得分:1)

只需通过链接文字找到下一个按钮

driver.find_element_by_link_text(">").click()

完成工作代码(包括窗口最大化和等待):

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC


url = 'https://webapps.cityofchicago.org/activegcWeb/'
driver = webdriver.Firefox()
wait = WebDriverWait(driver, 10)

driver.maximize_window()
driver.get(url)

# click next
wait.until(EC.visibility_of_element_located((By.LINK_TEXT, ">"))).click()

答案 1 :(得分:1)

使用XPath定位器" // [包含(@href,' headerPaginator:next')]"然后点击。