首页 文章

使用Selenium WebDriver和Java滚动

提问于
浏览
10

我正在使用Selenium WebDriver自动化我的浏览器测试 . My browser header is floating and is always present irrespective of the browser scroll .

因此,当我单击浏览器当前可见区域下方的某些元素时,selenium会尝试将元素滚动到视图中并单击它们 .

但是由于自动滚动,因此元素在浮动 Headers 后面滚动,当对它们执行任何操作时,页面 Headers 中的元素被单击 .

is there any way to limit the default scroll of the WebDriver

8 回答

  • 4

    如果你想使用selenium webdriver滚动firefox窗口,其中一种方法是在java代码中使用javaScript,向下滚动的javeScript代码如下:

    JavascriptExecutor js = (JavascriptExecutor)driver;
                        js.executeScript("window.scrollTo(0,Math.max(document.documentElement.scrollHeight," +
                        "document.body.scrollHeight,document.documentElement.clientHeight));");
    
  • 5
    Locatable hoverItem = (Locatable) driver.findElement(By.xpath("//li[text()='Reklama w Google']"));
        int y = hoverItem.getCoordinates().getLocationOnScreen().getY();
        ((JavascriptExecutor)driver).executeScript("window.scrollBy(0,"+y+");");
    
  • 1

    您可以使用javascript滚动到必要的位置您需要使用scrollTo方法而不是scrollBy方法才能使用它 .

    public void scrollToElement(By by) {
        Locatable element = (Locatable) selenium.findElement(by);
        Point p= element.getCoordinates().getLocationOnScreen();
        JavascriptExecutor js = (JavascriptExecutor) selenium;  
        js.executeScript("window.scrollTo(" + p.getX() + "," + (p.getY()+150) + ");");
    }
    
  • 2

    简单地使用 .sendKeys(Keys.PAGE_DOWN); 当你的元素可见时,只需单击它,通过 .click(element).perform(); 为我工作这样的事情:

    clicker = new Actions(driver);
        clicker.sendKeys(Keys.PAGE_DOWN);
        Thread.sleep(1000);
        clicker.click(button).perform();     
        Thread.sleep(1000);
    
  • 2

    滚动到顶部可以完成:

    private void scrollToTop() {
        JavascriptExecutor js = (JavascriptExecutor) webDriver;
        js.executeScript("window.scrollTo(0, 0);");
    }
    
  • 0

    向下滚动:

    System.setProperty("webdriver.chrome.driver",
                       "/home/shreetesh/chromedriver");
    WebDriver driver = new ChromeDriver(); 
    String url = "https://en.wikipedia.org/wiki/Main_Page";
    driver.get(url);
    JavascriptExecutor js = (JavascriptExecutor)driver;
    js.executeScript("scroll(0, 25000);");
    

    要向上滚动,只需用(2500,0)替换scroll的值 .

  • 1

    使用下面的代码向上滚动和向下滚动

    Actions dragger = new Actions(driver);
    
    WebElement draggablePartOfScrollbar = driver.findElement(By.xpath("<Scroll bar Element >"));
    
    // drag downwards
    
    int numberOfPixelsToDragTheScrollbarDown = 50;
    
    for (int i=10 ; i<500 ; i=i+numberOfPixelsToDragTheScrollbarDown) {
        try {
            // this causes a gradual drag of the scroll bar, 10 units at a time
            dragger.moveToElement(draggablePartOfScrollbar).clickAndHold().moveByOffset(0,numberOfPixelsToDragTheScrollbarDown).release().perform();
            Thread.sleep(1000L);
        } catch(Exception e1){}
    } 
    
    // now drag opposite way (downwards)
    numberOfPixelsToDragTheScrollbarDown = -50;
    for (int i=500;i>10;i=i+numberOfPixelsToDragTheScrollbarDown){
        // this causes a gradual drag of the scroll bar, -10 units at a time
        dragger.moveToElement(draggablePartOfScrollbar).clickAndHold().moveByOffset(0,numberOfPixelsToDragTheScrollbarDown).release().perform();
        Thread.sleep(1000L);
    }
    
  • 1

    我最近遇到了这个问题,因为在运行此代码时,Drupal菜单阻止了该元素:

    public void scrollTo(WebElement x) {
            ((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", x);
            }
    

    referencing this page之后,我更新了使用此代码将布尔值设置为false,并且它工作得很好:

    public void scrollTo(WebElement x) {
            ((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(false);", x);
            }
    

相关问题