我正在尝试在此网站上的搜索框中使用硒: https://www.claytoncountyga.gov/government/sheriff/inmate-search
def clayton_search(last, first, middle):
print("Clayton County Jail")
url = "https://www.claytoncountyga.gov/government/sheriff/inmate-search"
driver = webdriver.Chrome(chrome_options=(), executable_path="/usr/bin/chromedriver")
driver.get(url)
wait = WebDriverWait(driver, 30)
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "#name")))
driver.find_element_by_css_selector("#name").send_keys(last," ",first, " ", middle, Keys.ENTER)
return driver
driver = clayton_search(last, first, middle)
该网站使用angularJS,而且我知道在angularJS网站上,硒不会看到元素,除非您告诉它等到元素可见为止。如:
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "#name")))
但是我收到超时异常。我试图通过CSS选择器,XPATH和ID查找元素。
即使堆栈跟踪没有表明它可能隐藏在另一个元素后面。我也尝试过使用:driver.execute_script
我以为导航栏上的弹出菜单可能会覆盖它,但是它仅在光标位于导航栏上时出现。
为什么我不能使用wait.until(EC.visibility_of_element_located
在此AngularJS网站上找到元素?
答案 0 :(得分:1)
有一个iframe阻止您访问该元素。您必须先切换iframe。请尝试以下代码。
def clayton_search(last, first, middle):
print("Clayton County Jail")
url = "https://www.claytoncountyga.gov/government/sheriff/inmate-search"
driver = webdriver.Chrome(chrome_options=(), executable_path="/usr/bin/chromedriver")
driver.get(url)
wait = WebDriverWait(driver, 30)
wait.until(EC.frame_to_be_available_and_switch_to_it((By.ID,"Clayton County")))
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "#name")))
driver.find_element_by_css_selector("#name").send_keys(last," ",first, " ", middle, Keys.ENTER)
return driver
答案 1 :(得分:1)
此页面的#name
中有iframe
,而Selenium
不在iframe
中搜索。
您必须先switch_to.frame()
,然后才能在iframe
中搜索
import selenium.webdriver
url = "https://www.claytoncountyga.gov/government/sheriff/inmate-search"
driver = selenium.webdriver.Firefox()
driver.get(url)
iframes = driver.find_elements_by_tag_name('iframe')
#print('iframes:', iframes)
driver.switch_to.frame(iframes[0])
item = driver.find_element_by_id('name')
#print('name:', item)
item.send_keys("John")
item = driver.find_element_by_name('NameSearch')
#print('name:', item)
item.click()