Scroll Element into View with Selenium

前端 未结 30 2312
时光说笑
时光说笑 2020-11-22 08:31

Is there any way in either Selenium 1.x or 2.x to scroll the browser window so that a particular element identified by an XPath is in view of the browser? There is a focus m

相关标签:
30条回答
  • 2020-11-22 08:59

    The Ruby script for scrolling an element into view is as below.

    $driver.execute_script("arguments[0].scrollIntoView(true);", element)
    sleep(3)
    element.click
    
    0 讨论(0)
  • 2020-11-22 09:00

    Sometimes I also faced the problem of scrolling with Selenium. So I used javaScriptExecuter to achieve this.

    For scrolling down:

    WebDriver driver = new ChromeDriver();
    JavascriptExecutor js = (JavascriptExecutor)driver;
    js.executeScript("window.scrollBy(0, 250)", "");
    

    Or, also

    js.executeScript("scroll(0, 250);");
    

    For scrolling up:

    js.executeScript("window.scrollBy(0,-250)", "");
    

    Or,

    js.executeScript("scroll(0, -250);");
    
    0 讨论(0)
  • 2020-11-22 09:02

    If you think other answers were too hacky, this one is too, but there is no JavaScript injection involved.

    When the button is off the screen, it breaks and scrolls to it, so retry it... ¯\_(ツ)_/¯

    try
    {
        element.Click();
    }
    catch {
        element.Click();
    }
    
    0 讨论(0)
  • 2020-11-22 09:02

    Selenium can scroll to some element in the scrollbar automatically for some simple UI, but for lazy-load UI, scrollToElement is still needed.

    This is my implementation in Java with JavascriptExecutor. You can find more details in Satix source code: http://www.binpress.com/app/satix-seleniumbased-automation-testing-in-xml/1958

    public static void perform(WebDriver driver, String Element, String ElementBy, By by) throws Exception {
     try {
      //long start_time = System.currentTimeMillis();           
      StringBuilder js = new StringBuilder();
      String browser = "firefox";
    
      if (ElementBy.equals("id")) {
       js.append("var b = document.getElementById(\"" +
        Element + "\");");
      } else if (ElementBy.equals("xpath")) {
       if (!"IE".equals(browser)) {
        js.append("var b = document.evaluate(\"" +
         Element +
         "\", document, null, XPathResult.ANY_TYPE, null).iterateNext();");
       } else {
        throw new Exception("Action error: xpath is not supported in scrollToElement Action in IE");
       }
      } else if (ElementBy.equals("cssSelector")) {
       js.append("var b = document.querySelector(\"" +
        Element + "\");");
      } else {
       throw new Exception("Scroll Action error");
      }
    
      String getScrollHeightScript = js.toString() + "var o = new Array(); o.push(b.scrollHeight); return o;";
    
      js.append("b.scrollTop = b.scrollTop + b.clientHeight;");
      js.append("var tmp = b.scrollTop + b.clientHeight;");
      js.append("var o = new Array(); o.push(tmp); return o;");
    
      int tries = 1;
      String scrollTop = "0";
      while (tries > 0) {
       try {
        String scrollHeight = ((JavascriptExecutor) driver).executeScript(getScrollHeightScript).toString();
        if (scrollTop.equals(scrollHeight)) {
         break;
        } else if (driver.findElement(by).isDisplayed()) {
         break;
        }
        Object o = ((JavascriptExecutor) driver).executeScript(js.toString());
        scrollTop = o.toString();
        Thread.sleep(interval);
        tries++;
       } catch (Exception e) {
        throw new Exception("Action error:" +
         " javascript execute error : " + e.getMessage() + ", javascript : " + js.toString());
       }
      }
    
     } catch (Exception e) {
      try {
       ScreenshotCapturerUtil.saveScreenShot(driver, CLASSNAME);
      } catch (IOException e1) {
       throw new Exception("Save screenshot error!", e1);
      }
      throw e;
     }
    }
    
    0 讨论(0)
  • 2020-11-22 09:05

    If you want to scroll on the Firefox window using the Selenium webdriver, one of the ways is to use JavaScript in the Java code. The JavaScript code to scroll down (to bottom of the web page) is as follows:

    JavascriptExecutor js = (JavascriptExecutor) driver;
    js.executeScript("window.scrollTo(0, Math.max(document.documentElement.scrollHeight, document.body.scrollHeight, document.documentElement.clientHeight));");
    
    0 讨论(0)
  • 2020-11-22 09:06

    I have used this way for scrolling the element and click:

    List<WebElement> image = state.getDriver().findElements(By.xpath("//*[contains(@src,'image/plus_btn.jpg')]"));
    
    for (WebElement clickimg : image)
    {
      ((JavascriptExecutor) state.getDriver()).executeScript("arguments[0].scrollIntoView(false);", clickimg);
      clickimg.click();
    }
    
    0 讨论(0)
提交回复
热议问题