gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 09:49:51 26 4
gpt4 key购买 nike

关于这个主题有很多东西可以找到,但无法弄清楚。我需要滚动到(不太长)无限滚动页面的末尾。我有 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com