[英]Page scroll up or down in Selenium WebDriver (Selenium 2) using java
[英]Why am I unable to scroll down the page using Selenium 2?
我已经安装了壁虎驱动程序。
我在页面向下滚动时有点困惑。控制台没有向我显示任何错误,因为我写了测试用例失败如果(如果条件失败):
package PackageQandle;
//import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebElement;
//import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
//import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
//import junit.framework.Assert;
public class Adduser {
public static void main(String[] args) throws Throwable {
System.setProperty("webdriver.gecko.driver","C:/Users/sudhir/geckodriver-v0.18.0-win32/geckodriver.exe");
FirefoxDriver driver = new FirefoxDriver();
driver.get("https://prod4.qandle.com");
WebDriverWait webwait = new WebDriverWait(driver,120);
webwait.until(ExpectedConditions.elementToBeClickable(driver.findElementByXPath(".//*[@id='login-email']")));
WebElement web = driver.findElementByXPath(".//*[@id='login-email']");
web.sendKeys("Anil@gmail.com");
WebDriverWait webwait1 = new WebDriverWait(driver,20);
webwait1.until(ExpectedConditions.elementToBeClickable(driver.findElementByXPath(".//*[@id='login-password']")));
WebElement web1 = driver.findElementByXPath(".//*[@id='login-password']");
web1.sendKeys("Abc12345");
WebElement web2 = driver.findElementByXPath(".//*[@id='signInSubmit']");
web2.submit();
//Assert.assertEquals(my_Title, my_ExpectedTitle);
Thread.sleep(5000);
//JavascriptExecutor j = new JavascriptExecutor();
String my_Title = driver.getCurrentUrl();
//System.out.println(my_Title);
String my_ExpectedTitle = "https://prod4.qandle.com/#/";
if(my_Title.equals(my_ExpectedTitle)){
driver.executeScript("Scroll(0,600);");
}else{
System.out.println("Test Case Failed");
}
}
}
我正在使用此代码检查向下滚动页面时出现的元素。
您需要将驱动程序转换为 JavascriptExecutor 类型
JavascriptExecutor jse = (JavascriptExecutor) deiver;
WebElement scrollElement = driver.findElement();
jse.executeScript("return arguments[0].scrollIntoView();", scrollElement);
findElementByXpath 的语法必须是
driver.findElement(By.xpath(".//*[@id='login-password']"));
尝试下面提到的滚动到元素的代码,它对我有用
driver.get("https://prod4.qandle.com");
WebDriverWait webwait = new WebDriverWait(driver,120);
webwait.until(ExpectedConditions.elementToBeClickable(driver.findElement(By.xpath(".//*[@id='login-email']"))));
WebElement web = driver.findElement(By.xpath(".//*[@id='login-email']"));
web.sendKeys("Anil@gmail.com");
WebDriverWait webwait1 = new WebDriverWait(driver,20);
webwait1.until(ExpectedConditions.elementToBeClickable(driver.findElement(By.xpath(".//*[@id='login-password']"))));
WebElement web1 = driver.findElement(By.xpath(".//*[@id='login-password']"));
web1.sendKeys("Abc12345");
WebElement web2 = driver.findElement(By.xpath(".//*[@id='signInSubmit']"));
web2.submit();
//Assert.assertEquals(my_Title, my_ExpectedTitle);
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String my_Title = driver.getCurrentUrl();
String my_ExpectedTitle = "https://prod4.qandle.com/#/";
if(my_Title.equals(my_ExpectedTitle)){
JavascriptExecutor js = (JavascriptExecutor) driver;
// Mention the xpath of the element to be scrolled for
WebElement tempElement=driver.findElement(By.xpath("//*[contains(text(),'Reports')]"));
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", tempElement);
}else{
System.out.println("Test Case Failed");
}
如果您使用的是“Chrome”,请使用:
js.ExecuteScript("arguments[0].scrollIntoViewIfNeeded(true);", e)
对于“Firefox”和“IE”使用:
js.ExecuteScript("arguments[0].scrollIntoView(true);" +
"window.scrollBy(0,-100);", e);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.