PHP8.1.21版本已发布
vue8.1.21版本已发布
jquery8.1.21版本已发布

如何使用Java中的Selenium WebDriver向下滚动?

WBOY
WBOY 转载
2023-08-20 16:41:28 644浏览

我们可以使用Selenium向下滚动。Selenium无法直接处理滚动操作,它需要借助Javascript Executor来执行滚动操作,直到滚动到指定元素。

首先,我们需要定位到要滚动到的元素。接下来,我们将使用Javascript Executor来运行Javascript命令。在Selenium中,使用executeScript方法来运行Javascript命令。我们将借助Javascript中的scrollIntoView方法,并将true作为参数传递给该方法。

语法

WebElement elm = driver.findElement(By.name("name"));
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);",elm);

Example

的中文翻译为:

示例

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.JavascriptExecutor;
public class ScrollAction{
   public static void main(String[] args) {
      System.setProperty("webdriver.chrome.driver",
      "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe");
      WebDriver driver = new ChromeDriver();
      driver.get("https://www.tutorialspoint.com/about/about_careers.htm ");
      driver.manage().timeouts().implicitlyWait(4, TimeUnit.SECONDS);
      // identify element
      WebElement n=driver.findElement(By.xpath("//*[text()='Contact']"));
      // Javascript executor
      ((JavascriptExecutor)driver).executeScript("arguments[0].scrollIntoView
      (true);", n);
   }
}

输出

如何使用Java中的Selenium WebDriver向下滚动?

以上就是如何使用Java中的Selenium WebDriver向下滚动?的详细内容,更多请关注php中文网其它相关文章!

声明:本文转载于:tutorialspoint,如有侵犯,请联系admin@php.cn删除