我正尝试通过以下方式抓取Google新闻页面:
from selenium import webdriver
import time
from pprint import pprint
base_url = 'https://www.google.com/'
driver = webdriver.Chrome('/home/vincent/wintergreen/chromedriver') ## change here to your location of the chromedriver
driver.implicitly_wait(30)
driver.get(base_url)
input = driver.find_element_by_id('lst-ib')
input.send_keys("brexit key dates timetable schedule briefing")
click = driver.find_element_by_name('btnK')
click.click()
news = driver.find_element_by_link_text('News')
news.click()
tools = driver.find_element_by_link_text('Tools')
tools.click()
time.sleep(1)
recent = driver.find_element_by_css_selector('div.hdtb-mn-hd[aria-label=Recent]')
recent.click()
# custom = driver.find_element_by_link_text('Custom range...')
custom = driver.find_element_by_css_selector('li#cdr_opt span')
custom.click()
from_ = driver.find_element_by_css_selector('input#cdr_min')
from_.send_keys("9/1/2018")
to_ = driver.find_element_by_css_selector('input#cdr_max')
to_.send_keys("9/2/2018")
time.sleep(1)
go_ = driver.find_element_by_css_selector('form input[type="submit"]')
print(go_)
pprint(dir(go_))
pprint(go_.__dict__)
go_.click()
此脚本设法输入搜索词,切换到新闻选项卡,打开自定义时间段选项卡,填写开始和结束日期,但此后无法单击“开始”按钮。
从脚本末尾的print
和pprint
语句中,我可以推断出它确实可以成功找到'go'按钮,但无法以某种方式单击它。错误显示为selenium.common.exceptions.ElementNotVisibleException: Message: element not visible
任何有Selenium经验的人都可以快速运行它,并给我提示为什么它会返回这样的错误吗?
谢谢!
答案 0 :(得分:3)
在chrome中使用开发人员工具评估CSS会产生4个元素。
改用以下CSS:
go_ = driver.find_element_by_css_selector('#cdr_frm > input.ksb.mini.cdr_go')