簡體   English   中英

使用Java中的Selenium WebDriver找到的重定向URL不是最終頁面

[英]Redirected url found was not the final page using selenium webdriver in java

我有一個如下的html頁面:

..

登錄
使用myPass

..

在Java中使用Selenium Webdriver,我嘗試了以下操作:

 WebDriver driver = new HtmlUnitDriver(); String url = "http://www.pic.net.sh/index.html"; // And now use this to visit Google driver.get(url); //driver.click("xpath=//a[contains(@href,'listDetails.do') //driver.findElement(By.xpath("//div[@class='quickActionHolderInfo loginAccountPass']/a[contains(text(), 'Login')]")).click(); driver.findElement(By.xpath("//div[@class='span12 quickActionHolder']/a[@href='/organisation/pass/login']")).click(); 

但是此頁面將重定向到另一個頁面,隨后,當我嘗試以下方法查找最終重定向的網址時

System.out.println("Page title is: " + driver.getCurrentUrl());

單擊鏈接后,它將顯示立即頁面的URL,而不顯示最后一個。

我是否錯過了任何東西,或者我的代碼在延遲重定向后是否不足以處理頁面的重定向? 我對此有些懷疑,但無濟於事,如果有人能指出我類似的問題,我將不勝感激。

通常是由於延遲調用新URL而發生的。 有多種方法可以解決此問題:

  • 嘗試等待最后一頁上的某些元素,然后執行getCurrentUrl()。
  • 另一種方法是應用while循環,直到當前URL更改為止。

我使用以下代碼進行測試:

int i=0;
do{
    try {
    Thread.sleep(200);
    } catch (InterruptedException e) {
    }
    i++;
} while(driver.getCurrentUrl().equals("http://www.pic.net.sh/index.html")&&i<10);

兩種方法都應該起作用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM