Selenium + python + java页面滑动操作

在做selenium web自动化的时候,经常会遇到一个这样的场景,某些元素在页面的底部,需要滚动页面使其可见。可以借助js来实现该场景

代码(python)

def scroll_element(self, driver, element):    # 滑动滚动条到某个指定的元素    js4 = "arguments[0].scrollIntoView();"    driver.execute_script(js4, element)
  • 滑动到页面的最顶部:“var q=document.documentElement.scrollTop=0”
  • 滑动到页面的最底部:“window.scrollTo(0,document.body.scrollHeight);”

代码(java)

public static void scrollToSpecifiedElement(WebDriver driver, WebElement element){    Point location = element.getLocation();    String s = "window.scrollBy(" + location.getX() + "," + location.getY() + ")";    ((JavascriptExecutor) driver).executeScript(s);}
发表评论
留言与评论(共有 0 条评论) “”
   
验证码:

相关文章

推荐文章