Java를 사용하여 Selenium에서 웹 페이지를 아래로 스크롤할 수 있습니다. Selenium은 스크롤을 직접 처리할 수 없습니다. 요소까지 스크롤 작업을 수행하려면 Javascript Executor의 도움이 필요합니다.
우선 스크롤해야 하는 요소를 찾아야 합니다. 다음으로 Javascript Executor를 사용하여 Javascript 명령을 실행합니다. executeScript 메소드는 Selenium에서 Javascript 명령을 실행하는 데 사용됩니다. 우리는 자바스크립트의 scrollIntoView 메소드의 도움을 받아 메소드에 대한 인수로 true를 전달할 것입니다.
구문 -
WebElement elm = driver.findElement(By.name("name"));
((JavascriptExecutor) driver)
.executeScript("arguments[0].scrollIntoView(true);", elm); 예
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.JavascriptExecutor;
public class ScrollAction{
public static void main(String[] args) {
System.setProperty("webdriver.gecko.driver", "C:\\Users\\ghs6kor\\Desktop\\Java\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
//implicit wait
driver.manage().timeouts().implicitlyWait(4, TimeUnit.SECONDS);
//launch application
driver.get
("https://www.tutorialspoint.com/about/about_careers.htm ");
// identify element
WebElement n=driver.findElement(By.xpath("//*[text()='Contact']"));
// Javascript executor
((JavascriptExecutor)driver)
.executeScript("arguments[0].scrollIntoView(true);", n);
}
} 출력
