2017-10-13 40 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"); 
}