使用Python Selenium调用简单的滚动操作不起作用:
driver = webdriver.Chrome()
driver.get('https://www.wikipedia.org/')
time.sleep(2)
actions = ActionChains(driver)
actions.move_by_offset(500, 500).perform()
例如移动到元素的功能,有效“确定并滚动”:
driver = webdriver.Chrome()
driver.get('https://www.wikipedia.org/')
time.sleep(2)
element = driver.find_element_by_css_selector(<Something>)
actions = ActionChains(driver)
actions.move_to_element(element).perform()
调用移动到具有偏移的元素,不再起作用:
driver = webdriver.Chrome()
driver.get('https://www.wikipedia.org/')
time.sleep(2)
element = driver.find_element_by_css_selector(<Something>)
actions = ActionChains(driver)
actions.move_to_element_with_offset(element, 500, 500).perform()
为什么?
答案 0 :(得分:2)
尝试在移动鼠标后等待几秒钟。例如,以下代码可在我的CentOS7.3主机中获取屏幕截图。
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--headless')
chrome_options.add_argument("--window-size=720,480")
chrome_options.add_argument('--no-sandbox')
driver = webdriver.Chrome('/usr/bin/chromedriver', chrome_options=chrome_options, service_args=['--verbose', '--log-path=/tmp/chromedriver.log'])
driver.get(url)
time.sleep(6)
ActionChains(driver).move_by_offset(50, 50).perform()
time.sleep(2)
filename="/tmp/Screenshots/uuid.png"
driver.save_screenshot(filename)
答案 1 :(得分:0)
似乎@Extended = 'title1, title2, title3'
@Exclude = 'title5, title8'
无法滚动页面,但仍可以将鼠标移动到当前鼠标位置的偏移处。
要确认我们可以尝试执行以下操作:
move_by_offset
要按偏移量滚动页面,我们必须使用js:
driver = webdriver.Chrome()
driver.get('https://www.wikipedia.org/')
actions = ActionChains(driver)
actions.move_by_offset(300, 500).context_click().perform()