python - 具有无头模式的docker容器中的python selenium + geckodriver无法滚动页面
问题描述
我正在尝试在 docker 容器 ubuntu 18.04 中以无头模式使用 geckodriver 运行 selenium。这是我的代码:
while True:
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
time.sleep(2)
newHeight = driver.execute_script("return document.body.scrollHeight")
print('scrolling..')
if newHeight == lastHeight:
print(f'scrolling done..')
list_of_images = driver.find_elements_by_css_selector('._2eea a')
print(f'collecting: {fp_url}')
images = []
for image in list_of_images:
url = image.get_attribute('href')
if 'type=3' in str(url):
print(f'append: {url}')
images.append(url)
# clear_memory()
print(f'total: {len(images)} memes')
count = 1
else:
lastHeight = newHeight
当我在本地计算机上尝试时,我没有收到任何错误,但是当我在 docker 容器中尝试时,页面似乎不会滚动。这是我的驱动程序设置:
options = webdriver.FirefoxOptions()
options.add_argument('--hide-scrollbars')
options.add_argument('--disable-gpu')
options.add_argument('-headless')
driver = webdriver.Firefox(firefox_options=options, executable_path=os.path.join(os.getcwd(), "geckodriver"))
解决方案
假设您的 chrome 驱动程序完全是最新的,但值得检查
https://sites.google.com/a/chromium.org/chromedriver/downloads
也许尝试几种不同的滚动方式可以产生更好的结果:)
使用动作链 - https://selenium-python.readthedocs.io/api.html
from selenium.webdriver.common.action_chains import ActionChains as AC
ele = driver.find_element_by_id("myID")
actions = AC(driver)
actions.move_to_element(ele).perform()
将元素 id 作为参数传递给 scrollintoview()
driver.execute_script("arguments[0].scrollIntoView();", ele)
推荐阅读
- laravel - 如何保存多选值?
- c - libelf 例如缺少 vis.h 标头
- java - java 2d 数组上的 java.lang.ArrayIndexOutOfBoundsException
- postgresql - 大 COUNT DISTINCT 在 postgresql 中执行缓慢
- python - 如何在字符串拆分中找到双倍间距?
- mysql - Docker MySQL 没有创建用户
- javascript - JavaScript“document.getElementById().innerHTML”在循环中等待
- wordpress - 无法在谷歌应用引擎上安装 wordpress 插件
- c - 使用非 NULL 指针时出现分段错误
- batch-file - 需要根据笔式驱动器和文件的可用性将文件提取到笔式驱动器中