小编use*_*711的帖子

带硒的无头镀铬,只能找到非无头滚动的方法

关于这个主题有很多东西可以找到,但无法弄清楚。我需要滚动到(不是很长)无限滚动页面的末尾。我有 2 个选项可以与 chrome non-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
Run Code Online (Sandbox Code Playgroud)

在意识到我无法在无头模式下逃脱之后的第二个黑客工作:

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
Run Code Online (Sandbox Code Playgroud)

所以两者都在 chrome 中工作,但不在无头模式下工作,我需要将它们推送到 ubuntu …

python selenium google-chrome headless

5
推荐指数
1
解决办法
6101
查看次数

标签 统计

google-chrome ×1

headless ×1

python ×1

selenium ×1