java,selenium web driver
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java,selenium web driver相关的知识,希望对你有一定的参考价值。
我是Selenium webdriver的新手。我编写了这段代码以找出导航命令但是一旦浏览器打开,就会显示一个登录弹出窗口。我尝试使用classname或xpath关闭它,但发生超时异常。
在这种情况下我是否需要使用显式等待?你能帮我弄清问题是什么吗?
public class TestNavigateCommands {
WebDriver driver;
public void invokeBrowser(){
try {
System.setProperty("webdriver.chrome.driver", "/Users/himaja/Documents/chromedriver");
ChromeOptions options=new ChromeOptions();
options.addArguments("start-fullscreen");
driver=new ChromeDriver(options);
driver.manage().deleteAllCookies();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(40, TimeUnit.SECONDS);
navigateCommands();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void navigateCommands(){
try {
driver.navigate().to("https://www.flipkart.com/");
Thread.sleep(4000);
driver.findElement(By.className("2AkmmA _29YdH8")).click();
//driver.findElement(By.xpath("//*[@class='_2AkmmA _29YdH8']")).click();
driver.findElement(By.xpath("//span[starts-with(text(),'Applicances')]")).click();
driver.findElement(By.xpath("//span[contains(text(),'Microwave Ovens')]")).click();
Thread.sleep(2000);
driver.navigate().back();
Thread.sleep(2000);
driver.navigate().forward();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
TestNavigateCommands test1= new TestNavigateCommands();
test1.invokeBrowser();
}
}
例外:
[43.366] [严重]:超时从渲染器接收消息:37.150 [43.373] [严重]:超时从渲染器接收消息:-0.007 org.openqa.selenium.TimeoutException:timeout
答案
试试这个代码可能有所帮助:
public class TestNavigateCommands {
public static void main(String[] args) throws InterruptedException {
try {
System.setProperty("webdriver.chrome.driver", "/Users/himaja/Documents/chromedriver");
WebDriver driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
WebDriverWait wait=new WebDriverWait(driver,50 );
driver.manage().window().maximize();
driver.navigate().to("https://www.flipkart.com/");
driver.findElement(By.xpath("//button[contains(@class,'YdH8')]")).click();
wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.xpath("//a[@title='Appliances']//span"))));
driver.findElement(By.xpath("//a[@title='Appliances']//span")).click();
} catch (Exception e) {
e.printStackTrace();
}
}
}
另一答案
public class TestNavigateCommands {
WebDriver driver;
public void invokeBrowser() {
try {
System.setProperty("webdriver.chrome.driver", "/Users/himaja/Documents/chromedriver");
ChromeOptions options = new ChromeOptions();
options.addArguments("start-fullscreen");
driver = new ChromeDriver(options);
driver.manage().deleteAllCookies();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(60, TimeUnit.SECONDS);
navigateCommands();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void navigateCommands() {
try {
driver.navigate().to("https://www.flipkart.com/");
driver.findElement(By.xpath("//div[@class='_3Njdz7']//button[@class='_2AkmmA _29YdH8']")).click();
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.xpath("//a[@title='Appliances']//span"))));
driver.findElement(By.xpath("//a[@title='Appliances']//span")).click();
Thread.sleep(2000);
driver.navigate().back();
driver.navigate().forward();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
以上是关于java,selenium web driver的主要内容,如果未能解决你的问题,请参考以下文章