在 Selenium 中向下滚动一定量,Chrome,Python
Scroll down by a certain amount in Selenium, Chrome, Python
我最近从一个非常旧的版本升级到新的 Chromedriver 版本,它给我以前的代码带来了很多问题,其中一个更烦人的是 .scroll 命令现在似乎会产生错误,例如:
scroll_obj=selenium.webdriver.common.touch_actions.TouchActions(driver)
scroll_obj.scroll(0,scroll_value)
scroll_obj.perform()
它产生以下错误:
WebDriverException: Message: unknown command: Cannot call non W3C standard command while in W3C mode
是否有任何类似的操作可以让我只向下滚动固定的量而不是特定的文档高度?我只能找到向下滚动到设定位置的 Javascript 解决方案。
这应该对你有帮助:
horizontal_scroll = 0
vertical_scroll = 1000
driver.execute_script("window.scrollBy(horizontal_scroll , vertical_scroll );")
**To scroll down the web page by pixel.**
JavascriptExecutor js = (JavascriptExecutor) driver;
// This will scroll down the page by 1000 pixel vertical
js.executeScript("window.scrollBy(0,1000)");
**To scroll down the web page by the visibility of the element.**
JavascriptExecutor js = (JavascriptExecutor) driver;
//Find element by link text and store in variable "Element"
WebElement Element = driver.findElement(By.linkText("Linux"));
//This will scroll the page till the element is found
js.executeScript("arguments[0].scrollIntoView();", Element);
**To scroll down the web page at the bottom of the page.**
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("window.scrollTo(0, document.body.scrollHeight)");
我最近从一个非常旧的版本升级到新的 Chromedriver 版本,它给我以前的代码带来了很多问题,其中一个更烦人的是 .scroll 命令现在似乎会产生错误,例如:
scroll_obj=selenium.webdriver.common.touch_actions.TouchActions(driver)
scroll_obj.scroll(0,scroll_value)
scroll_obj.perform()
它产生以下错误:
WebDriverException: Message: unknown command: Cannot call non W3C standard command while in W3C mode
是否有任何类似的操作可以让我只向下滚动固定的量而不是特定的文档高度?我只能找到向下滚动到设定位置的 Javascript 解决方案。
这应该对你有帮助:
horizontal_scroll = 0
vertical_scroll = 1000
driver.execute_script("window.scrollBy(horizontal_scroll , vertical_scroll );")
**To scroll down the web page by pixel.**
JavascriptExecutor js = (JavascriptExecutor) driver;
// This will scroll down the page by 1000 pixel vertical
js.executeScript("window.scrollBy(0,1000)");
**To scroll down the web page by the visibility of the element.**
JavascriptExecutor js = (JavascriptExecutor) driver;
//Find element by link text and store in variable "Element"
WebElement Element = driver.findElement(By.linkText("Linux"));
//This will scroll the page till the element is found
js.executeScript("arguments[0].scrollIntoView();", Element);
**To scroll down the web page at the bottom of the page.**
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("window.scrollTo(0, document.body.scrollHeight)");