我有一个场景 - 当我点击我的测试网址(http://example.com)上的按钮( abcB )时,它会被重定向到不同的网址(http://yourname.xyz)并且当我点击一个按钮( xyzB )时,它会回到我通常的测试网址(http://example.com)并执行更多功能。请让我知道如何做这个Selenium Webdriver。
答案 0 :(得分:1)
这是一项简单而直接的任务。我正在写一些伪代码,因为你没有共享任何HTML代码以供参考。请使用以下代码并尝试。
driver.findElement(By.Xpath("<your xpath reference of button in first page>").click(); //to click on the button, and will navigate to target page
driver.getTitle();// to get the title to ensure you are in the correct page
driver.findElement(By.Xpath("<your xpath reference of button in second page>").click();
driver.getTitle();// to get the title to ensure that the browser is navigated back
答案 1 :(得分:0)
如果我理解没错,你的情况就像这样
driver.findElement(By.Xpath("Your xpath").click();
//wait for few second for loading site
for (String windows : wd.getWindowHandles()) {
wd.switchTo().window(windows);
if (wd.getCurrentUrl().startsWith(Link + "xyz.com")) {
//Your Operation
}
if (wd.getCurrentUrl().startsWith(Link+"yzx.com")) {
//Your Operation
}
}
这里我做了什么,我点击重定向链接。并等待几秒钟的网站加载。重定向后,我返回欲望标签链接开始,你可以给其他条件!!
希望它能帮到你
答案 2 :(得分:0)
首先点击第二个URL定位器匹配标题并返回到第一个URL,在此处执行相同的操作,您可以处理重定向的URL
您可以尝试此示例示例
WebDriver driver=new FirefoxDriver();
//Go to first URL and click on Download menu
driver.get("http://www.seleniumhq.org");
driver.findElement(By.xpath("//*[@id='menu_download']")).click();
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
//Click on the Source code to redirect to second URL
WebElement sourceCode=driver.findElement(By.xpath(".//*[@id='mainContent']/p[1]/a[2]"));
sourceCode.click();
//Get the title of SecondURL and match
String SecondUrl= driver.getTitle();
if(SecondUrl.contains("GitHub - SeleniumHQ/selenium: A browser automation framework and ecosystem."))
{
System.out.println("welcome to second URL");
}
//come back to First URL by click on link
driver.findElement(By.xpath("//a[contains(text(),'http://seleniumhq.org')]")).click();
//Get the title of FirstURL and match
String FirstUrl= driver.getTitle();;
if(FirstUrl.contains("Selenium - Web Browser Automation"))
{
System.out.println("welcome to First URL");
}