我正在尝试为不同类型的卷轴创建一个类(向上滚动,向下滚动,页面底部等等),但我不能这样做。
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.chrome.ChromeDriver;
public class ScrollPage {
public static void scrolldown(String element) throws Exception {
System.setProperty("webdriver.chrome.driver", "C:\\JAVA\\DRIVER\\chromedriver.exe");
ChromeDriver driver = new ChromeDriver();
//Scroll 1/4 página
JavascriptExecutor jse1 = (JavascriptExecutor) driver;
jse1.executeScript("scroll(0,250);");
}
/*
public static void scrolldown2(String element) throws Exception {
//Scroll hasta la mitad
JavascriptExecutor jse2 = (JavascriptExecutor) driver;
jse2.executeScript("scroll(0,500);");
}
public static void scrolldown3(String element) throws Exception {
//Scroll hasta el final
JavascriptExecutor jse3 = (JavascriptExecutor) driver;
jse3.executeScript("scroll(0,1000);");
}
*/
//Otra forma de Scroll hasta el final
/* JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("window.scrollTo(0,Math.max"
+ "(document.documentElement.scrollHeight,"
+ "document.body.scrollHeight,document.documentElement.clientHeight));");
public static void scrollup(String element) throws Exception {
//Scroll hasta arriba
JavascriptExecutor jse4 = (JavascriptExecutor) driver;
jse4.executeScript("scroll(1000,0);");
}
public static void slowmotion (String element) throws Exception {
//Scroll en cámara lenta
for (int second = 0;; second++) {
if(second >=60){
break;
}
((JavascriptExecutor) driver).executeScript("window.scrollBy(0,1000)", "");
}
}
*/
}
之后,在另一个课程中,我想调用一个特定的卷轴:
ScrollPage.scrolldown();
请帮助我。
答案 0 :(得分:1)
我在你的代码中发现了很多错误:
equals(...)
方法中弄乱驱动程序实例化。从中删除所有与滚动无关的内容。scrolldown
方法driver.get(<some url>)
滚动窗口250像素答案 1 :(得分:0)
这不是Selenium Java问题,这与面向对象编程有关。你熟悉java类的工作原理吗?
创建一个名为ScrollMethods的新类,并将所有滚动方法放在该类中。
在您的测试中,您只需要实例化该类:
ScrollMethods scrollMethods = new ScrollMethods();
现在您应该可以在测试类中调用Scroll方法。
答案 2 :(得分:0)
您使用了错误的Java Script命令来滚动窗口。
试试这个: -
exec
答案 3 :(得分:0)
我的代码中存在一些问题。
正确的是下一个:
package Util;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.chrome.ChromeDriver;
public abstract class ScrollPage {
public static void doScrollDown250(ChromeDriver driver)throws Exception{
JavascriptExecutor jse = driver;
jse.executeScript("scroll(0, 250);");
}
public static void doScrollDown500(ChromeDriver driver)throws Exception {
JavascriptExecutor jse2 = (JavascriptExecutor) driver;
jse2.executeScript("scroll(0,500);");
}
public static void doScrollUp(ChromeDriver driver)throws Exception {
JavascriptExecutor jse2 = (JavascriptExecutor) driver;
jse2.executeScript("scroll(0,000);");
}
public static void doScrolldown1000(ChromeDriver driver)throws Exception {
JavascriptExecutor jse3 = (JavascriptExecutor) driver;
jse3.executeScript("scroll(0,1000);");
}
//Slow Motion Down
public static void slowmotionDown(ChromeDriver driver) throws Exception {
for (int second = 0;; second++) {
if(second >=60){
break;
}
((JavascriptExecutor) driver).executeScript("window.scrollBy(0,13)", "");
}
}
//Slow Motion Up
public static void slowmotionUp(ChromeDriver driver) throws Exception {
for (int second = 0;; second++) {
if(second >=60){
break;
}
((JavascriptExecutor) driver).executeScript("window.scrollBy(1000,13)", "");
}
}
//From Left to Right
public static void Left2Right(ChromeDriver driver) throws Exception {
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("window.scrollBy(2000,0)", "");
}
//From Right to Left
public static void Right2Left(ChromeDriver driver) throws Exception {
JavascriptExecutor jsx = (JavascriptExecutor)driver;
jsx.executeScript("window.scrollBy(-2000,0)", "");
}
//Scroll Down the Element Web, in this case, Ver Detalles del Plan (Modify the Xpath)
public static void WebElement (ChromeDriver driver) throws Exception{
((JavascriptExecutor)driver).executeScript("arguments[0].scrollIntoView();",
driver.findElement(By.xpath("//*[@id='option1']/div[1]/div[5]/a")));
}
}
&#13;
之后,在另一个课程中,我们将使用以下句子调用这些方法中的任何一个:
ScrollPage.slowmotionDown(driver);
Thread.sleep(3000);
请记住声明导入和ChromeDriver
import org.openqa.selenium.chrome.ChromeDriver;
import Util.ScrollPage; public class banner1扩展了ScrollPage {
public static void main(String[] args) throws Exception {
System.setProperty("webdriver.chrome.driver", "C:\\JAVA\\DRIVER\\chromedriver.exe");
ChromeDriver driver = new ChromeDriver();
答案 4 :(得分:0)
对于Scrollpage slowmotion down,代码为:
public static void slowmotionDown(InternetExplorerDriver driver) throws Exception {
for (int second = 0;; second++) {
if(second >=60){
break;
}
((JavascriptExecutor) driver).executeScript("window.scrollBy(0,13)", "");
}
}
对于Scrollpage slowmotion up,代码为:
public static void slowmotionUp(InternetExplorerDriver driver) throws Exception {
for (int second = 0;; second++) {
if(second >=60){
break;
}
((JavascriptExecutor) driver).executeScript("window.scrollBy(1000,-13)", "");
}
}
这很好。