更新时间:2022-02-17 23:10:23
您需要 WebDriverWait
来确保元素 visibility_of_element_located
,然后滚动到 Searchable Database
部分,您可以通过 xpath
使用定位器.
You need WebDriverWait
to make sure the element visibility_of_element_located
, then scroll to Searchable Database
section, and you can use locator by xpath
.
请导入:
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
试试下面的代码.
chromedriver_path = r"C:Userspath ochromedriver.exe"
browser = webdriver.Chrome(executable_path=chromedriver_path)
url = "http://www.ncsl.org/research/transportation/autonomous-vehicles-legislative-database.aspx"
topics_xpath = "//div[@class='divTopicsSection1']//span//label[text()='All Topics']"
states_xpath = "//div[@class='divStatesSection1']//span//label[text()='All States']"
dBase_xpath = "//h4[text()='Searchable Database']"
browser.get(url)
WebDriverWait(browser, 10).until(expected_conditions.visibility_of_element_located((By.XPATH, topics_xpath)))
elem = browser.find_element_by_xpath(dBase_xpath)
browser.execute_script("arguments[0].scrollIntoView(true);", elem)
browser.find_element_by_xpath(topics_xpath).click()
browser.find_element_by_xpath(states_xpath).click()