python - 带 Selenium 的 headless (headless) Chrome ,只能找到滚动非 headless (headless)的方法

标签 python google-chrome selenium headless

关于这个主题有很多东西可以找到,但无法弄清楚。我需要滚动到(不太长)无限滚动页面的末尾。我有 2 个选项可以使用 chrome 非 headless (headless)但似乎不能 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

在意识到我无法在 headless 模式下摆脱上述问题之后的第二个 hack 工作:

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 中工作但不在 headless (headless)模式下,我需要将它们推送到 ubuntu vps,在那里它们需要 headless (headless),我知道 xvfb 选项但我很高兴我可以删除它并使用原生 chrome因为液滴没有太多内存。

编辑:刚刚尝试了这种方法,重点放在页脚中的一个元素上,也适用于非 headless (headless)但不适用于 headless (headless):

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

有人有不同的方法吗?

编辑 只是想知道是否可以在 headless (headless)模式下使用 chrome 进行滚动!

最佳答案

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

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

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

在这里发帖,这样下一篇就不会像我一样花时间了。

关于python - 带 Selenium 的 headless (headless) Chrome ,只能找到滚动非 headless (headless)的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48257870/

相关文章:

python - 如何在 python webdriver 中为 chrome 设置代理?

jquery - 使用 Selenium 测试 jQuery Drag & Drop 和 Droppable

Python 将值写入dbf文件

python - 在线程中的 Qdialog 上调用 exec() 效果不佳

python - 如何添加计时器参数以从AWS在github上执行代码?

python - Selenium 不会返回文本

Java:字符串等于不适用于innerHTML 文本

python - 通过 Mixin 调用 Grandparent Method 而不执行父方法

javascript - 将参数传递给 jquery 回调,但也可以在外部使用回调

php - Chrome 正在创建具有相同 ID 的重复 session