我试图抓取这个网站论坛的整个html网页。只有在向下滚动之后,才能加载注释部分。滚动一些后,您会发现(在第4页上)最终会出现一个Load Next Page按钮,您必须单击该按钮才能得到后续的注释。经过多次搜索后,下面的代码可以很好地获得注释的最后一页。其中大部分内容也是从这个堆栈溢出帖子和这 one中提取的。
供参考,我是在Windows 10和我的Chrome驱动程序版本是76.0.3809.132。我还使用PhantomJS只是为了查看哪个加载更快。两个驱动程序.exe文件都放在与我正在执行脚本的目录相同的目录中。到今天为止,我还没有遇到任何问题。
import selenium.webdriver as webdriver
from selenium.webdriver.chrome.options import Options
def scrollDownAllTheWay(driver):
last_height = driver.execute_script("return document.body.scrollHeight")
while True:
driver.execute_script("window.scrollTo(0, 100*document.body.scrollHeight);")
time.sleep(3)
if "Load next page</button>" in driver.page_source:
driver.find_element_by_css_selector('.myButton').click()
new_height = driver.execute_script("return document.body.scrollHeight")
if new_height == last_height:
break
last_height = new_height
#Load this and comment out chrome headless code below, if needed.
#driver = webdriver.PhantomJS()
#Chrome driver
options = Options()
options.add_argument("--headless")
driver = webdriver.Chrome(options=options)
driver.get("https://www.chessable.com/discussion/thread/58883/official-chessable-launch-schedule-2019/")
scrollDownAllTheWay(driver)当我用webdriver.PhantomJS()运行上面的脚本(用它替换Chrome部分)时,我没有问题。该函数一直运行到无头浏览器到达最后一页为止。太棒了。
当我使用webdriver.Chrome() headless运行下面的脚本时,会遇到以下错误:
ElementClickInterceptedException: Message: element click intercepted: Element <button id="load-next-comments" class="myButton">...</button> is not clickable at point (388, 23). Other element would receive the click: <div class="headerHolder">...</div> (Session info: headless chrome=76.0.3809.132)我找不到任何有用的东西来解决这个问题。更奇怪的是,如果禁用options.add_argument("--headless")部件(注释掉),页面就会很好地加载,并完成整个页面的滚动。我可以看到最后的点击执行在我的本地Chrome浏览器,然后看到它停止滚动和点击,当它已经完成。
问:为什么无头Chrome会话在这里不能正常工作,而非无头版本是?
编辑:--我刚刚找到了这个帖子,这可能会有潜在的帮助,但我不确定。
注意:我愿意使用FireFox()等其他浏览器驱动程序作为潜在的解决办法,但问题仍然存在。
发布于 2019-09-04 00:57:32
在这个按钮的顶部有一个元素,使得它不能点击。如果你改变了:
driver.find_element_by_css_selector('.myButton').click()至
driver.execute_script("document.querySelector('.myButton').click()")应该管用的。事实上,从javascript做任何事情都不是一个好主意,除非您是在"QA测试“
发布于 2019-09-04 13:11:18
JavaScript不是required.If,您在headless模式上设置了window-size,它将单击next_page button.Hope,这将有所帮助。
import selenium.webdriver as webdriver
from selenium.webdriver.chrome.options import Options
def scrollDownAllTheWay(driver):
last_height = driver.execute_script("return document.body.scrollHeight")
while True:
driver.execute_script("window.scrollTo(0, 100*document.body.scrollHeight);")
time.sleep(3)
if "Load next page</button>" in driver.page_source:
driver.find_element_by_css_selector('.myButton').click()
print('clicked')
new_height = driver.execute_script("return document.body.scrollHeight")
if new_height == last_height:
break
last_height = new_height
options = Options()
options.add_argument("--headless")
options.add_argument('window-size=1920x1080')
driver = webdriver.Chrome(options=options)
driver.get("https://www.chessable.com/discussion/thread/58883/official-chessable-launch-schedule-2019/")
scrollDownAllTheWay(driver)要验证代码是否工作,只需在截图之前或之后进行截图,您就会知道它是工作的。
import selenium.webdriver as webdriver
from selenium.webdriver.chrome.options import Options
def scrollDownAllTheWay(driver):
last_height = driver.execute_script("return document.body.scrollHeight")
i = 1
while True:
driver.execute_script("window.scrollTo(0, 100*document.body.scrollHeight);")
time.sleep(3)
if "Load next page</button>" in driver.page_source:
driver.save_screenshot("screenshot_{}.png".format(i))
i = i+1
driver.find_element_by_css_selector('.myButton').click()
driver.save_screenshot("screenshot_{}.png".format(i))
i = i + 1
print('clicked')
new_height = driver.execute_script("return document.body.scrollHeight")
if new_height == last_height:
break
last_height = new_height
options = Options()
options.add_argument("--headless")
options.add_argument('window-size=1920x1080')
driver = webdriver.Chrome(options=options)
driver.get("https://www.chessable.com/discussion/thread/58883/official-chessable-launch-schedule-2019/")
scrollDownAllTheWay(driver)发布于 2020-07-05 18:41:42
我在Chromedriver上也有同样的问题。
通过将以下选项添加到我的代码中,可以解决这个问题:
options.add_argument("--window-size=1920,1080")
options.add_argument("--start-maximized")
options.add_argument("--headless")PS:我在这里找到了解决方案:https://github.com/SeleniumHQ/selenium/issues/4685
https://stackoverflow.com/questions/57778734
复制相似问题