嘿,StackOverflow社区。我对网络爬虫很陌生。在从复选框中选择“ 所有州”,“所有主题”和“ 2017 ”年之后,我想从Click here for link网站上抓取数据。但是我不能让selenium.webdriver实现这一动作。我使用从StackOverflow复制的以下代码:
wd = webdriver.Firefox(executable_path=curr_path+'/geckodriver')
wd.get(my_url)
for i in range(10):
try:
wd.find_element_by_id('dnn_ctr78525_BallotMeasuresDB_ckBxAllStates').click()
break
except NoSuchElementException as e:
print('retry in 1s.')
time.sleep(1)
错误是:ElementClickInterceptedException: Message: Element <input id="dnn_ctr78525_BallotMeasuresDB_ckBxAllStates" name="dnn$ctr78525$BallotMeasuresDB$ckBxAllStates" type="checkbox"> is not clickable at point (84.5,679.2666625976562) because another element <div id="psCookieSlider"> obscures it
然后我尝试了这个:
Create = wd.find_element_by_xpath("//*['dnn_ctr78525_BallotMeasuresDB_ckBxAllStates']")
WebDriverWait(driver, 10).until_not(EC.visibility_of_element_located((By.ID, "psCookieSlider")))
Create.click()
弹出的错误是这样的: BrokenPipeError:[Errno 32]管道损坏
我需要您的建议和帮助。非常感谢!
答案 0 :(得分:0)
您可以尝试以下代码:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.select import Select
driver = webdriver.Chrome(executable_path = r'D:/Automation/chromedriver.exe')
driver.maximize_window()
driver.get("http://www.ncsl.org/research/elections-and-campaigns/ballot-measures-database.aspx")
wait = WebDriverWait(driver, 30)
driver.execute_script("window.scrollTo(0, 450)")
all_state = wait.until(EC.element_to_be_clickable((By.XPATH, "//label[text()='All States']/preceding-sibling::input")))
all_state.click()
all_topics = wait.until(EC.element_to_be_clickable((By.XPATH, "//label[text()='All Topics']/preceding-sibling::input")))
all_topics.click()
year = Select(driver.find_element_by_css_selector("div.divBottomInterior>select[id*='BallotMeasuresDB_ddlYear']"))
year.select_by_visible_text("2017")
之后,如果您想单击搜索按钮,则可以遵循以下代码:
search_button = wait.until(EC.element_to_be_clickable((By.LINK_TEXT, "Search")))
search_button.click()