2017-10-13 102 views
-4

我有一种情况 - 当我点击我的测试URL(http://example.com)按钮(ABCB),它会当我点击被重定向到不同的URL(http://yourname.xyz),并有一个按钮(xyzB),它会回到我通常的测试网址(http://example.com),并执行进一步的功能。请让我知道我该如何做这个Selenium Webdriver。硒的webdriver - 最好的处理方法重定向的URL

+1

向我们展示您的代码。 –

+0

你在哪里遇到错误/问题?错误堆栈跟踪? – DebanjanB

回答

0

这是一个简单而直接的任务。我写了一些伪代码,因为你没有分享任何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 
+0

好的,谢谢。由于url会改变,我认为我们需要以不同的方式处理它。 –

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 
        } 


      } 

在这里,我所做的一切,我点击重定向link.And等待站点load.After重定向几秒钟,我渴望回归标签链接开始,在那里你可以给其他条件也!

希望它会帮助你

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"); 
}