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

2024-04-18 18:18:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用selenium浏览此页:

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

但我找不到如何移动到任何页面,使用chrome,我在“下一步”按钮中得到了这个Xpath:

'//*[@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]"}'

Tags: httpsorgidurldriverseleniumbutton页面
2条回答

带有XPath定位器“//a[包含(@href,'标题页码:下一页“)]”,然后单击。你知道吗

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

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()

相关问题 更多 >