python docker 无头模式容器中的 selenium + geckodriver 无法滚动页面
python selenium + geckodriver in docker container with headless mode cannot scroll page
我正尝试在 docker 容器 ubuntu 18.04 中以无头模式 运行 selenium 和 geckodriver。这是我的代码:
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)
我正尝试在 docker 容器 ubuntu 18.04 中以无头模式 运行 selenium 和 geckodriver。这是我的代码:
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)