【问题标题】:Headless chrome with selenium, can only find ways to scroll non-headless带硒的无头铬,只能想办法滚动非无头
【发布时间】:2018-06-23 18:45:45
【问题描述】:

关于这个主题有很多东西可以找到,但无法弄清楚。我需要滚动到(不是很长)无限滚动页面的末尾。我有 2 个选项可以与 chrome non-headless 一起使用,但似乎无法使用 headless。

我最喜欢的第一个,做工漂亮,在 SA 上找到:

driver = webdriver.Chrome('c:/cd.exe', chrome_options=chrome_options)
driver.get('http://www.website.com')

while True:
    count = len(driver.find_elements_by_xpath('//div[@itemprop="itemListElement"]'))
    print(count)
    driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
    try:
        WebDriverWait(driver, 50).until(EC.visibility_of_element_located((By.XPATH,
                                                                          "//div[@itemprop='itemListElement'][%s]" % str(count + 1))))
    except TimeoutException:
        break

在意识到我无法在无头模式下摆脱上述情况后进行第二次黑客工作:

driver = webdriver.Chrome('c:/cd.exe', chrome_options=chrome_options)
driver.get('https://www.website.com')

while True:

    count = len(driver.find_elements_by_xpath('//div[@itemprop="itemListElement"]'))
    actions = ActionChains(driver)
    actions.send_keys(Keys.PAGE_DOWN)
    actions.perform()
    actions.send_keys(Keys.PAGE_DOWN)
    actions.perform()


    # focus_element_scroll = driver.find_elements_by_xpath('//section[@class="occasion-content"]')
    # driver.find_elements_by_xpath('//div[@itemprop="itemListElement"]')[-1].send_keys(Keys.PAGE_DOWN)
    # driver.find_elements_by_xpath('//div[@itemprop="itemListElement"]')[-1].send_keys(Keys.PAGE_DOWN)
    # self.driver.find_element_by_css_selector("ul.list-with-results").send_keys(Keys.ARROW_DOWN)
    print(count)
    # driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")

    try:  
        WebDriverWait(driver, 50).until(EC.visibility_of_element_located((By.XPATH,
                                                                          "//div[@itemprop='itemListElement'][%s]" % str(count + 1))))
    except TimeoutException:
        break

所以两者都在 chrome 中工作,但不在无头模式下,我需要将它们推送到需要无头的 ubuntu vps,我知道 xvfb 选项,但我很高兴我可以删除它并使用原生 chrome因为水滴没有太多的记忆。

编辑:刚刚尝试了这种方法,专注于页脚中的一个元素,也适用于非无头但不适用于无头:

ActionChains(driver).move_to_element(focus[0]).perform()

有人有不同的方法吗?

编辑只是想知道是否可以在无头模式下使用chrome滚动!

【问题讨论】:

    标签: python google-chrome selenium headless


    【解决方案1】:

    在尝试了 selenium、chrome 和 chromedriver 的不同版本组合 2 天后找到了答案,我几乎放弃了并想使用 xvfb。

    已经尝试在 chrome 参数中最大化窗口,但没有帮助。但是这次我尝试设置手动窗口大小。这有帮助。

        chrome_options.add_argument("window-size=1920,1080")
    

    在这里发帖,这样下一个不会像我一样长。

    【讨论】:

      【解决方案2】:

      scrollend of the page 的(不太长)无限滚动Default Chrome BrowserHeadless Chrome Browser您可以使用以下代码块:

      from selenium import webdriver
      from selenium.webdriver.chrome.options import Options
      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.common.exceptions import TimeoutException
      
      options = Options()
      options.add_argument("--headless")
      options.add_argument("start-maximized")
      options.add_argument("disable-infobars")
      options.add_argument("--disable-extensions")
      options.add_argument("--no-sandbox")
      options.add_argument("--disable-dev-shm-usage")
      driver = webdriver.Chrome(chrome_options=options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
      driver.get('http://www.website.com')
      
      while (driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")):
          try:
              WebDriverWait(driver, 50).until(EC.visibility_of_element_located((By.XPATH, "//div[@itemprop='itemListElement']" )))
              # do your other actions within the Viewport
          except TimeoutException:
              break
      print("Reached to the bottom of the page")
      

      【讨论】:

      • 感谢遮阳篷,但这是我设置它的方式,它工作,机器人仅在非无头模式下,与我提出的其他 2 个解决方案相同。你试过这个代码吗?
      • 我没有收到您的评论。我的答案不是在 headless 模式下为你执行吗?代码在我的最后经过了很好的测试。
      • 代码在没有 --headless 参数的情况下运行良好。在 Windows 和我的 ubuntu vps 上,它不能在无头模式下工作......没有滚动的地方。
      • @user9108711 请在 Windows 上使用错误堆栈跟踪更新问题。
      • 没有错误,WebDriverWait 启动并且元素永远不会变得可见,因为没有滚动。同样,我可以看到它在没有无头参数的情况下清晰滚动。我要获取的页面:http://bit.ly/2mxdV1Z
      【解决方案3】:

      我刚刚在 Windows 上遇到了这个问题。使用 chrome 74,我通过以下 chromeOptions 解决了这个问题。我的无头模式再次起作用 :) 感谢 DebanjanB

      chromeOptions.addArguments("--headless")
      chromeOptions.addArguments("--no-sandbox")
      chromeOptions.addArguments("--disable-dev-shm-usage")
      chromeOptions.addArguments("--window-size=1920x1080")
      chromeOptions.addArguments("start-maximised")
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-04-28
        • 1970-01-01
        • 2019-08-05
        • 2018-04-14
        • 1970-01-01
        • 2017-12-23
        • 2019-12-12
        • 2018-06-22
        相关资源
        最近更新 更多