简体   繁体   English

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

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

i have a html page like below : 我有一个如下的html页面:

.. ..

Login 登录
with myPass 使用myPass

.. ..

Using selenium webdriver in Java, I tried the below: 在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(); 

BUT this page will redirect to another page and subsequent and when I tried the below to find the final redirected url 但是此页面将重定向到另一个页面,随后,当我尝试以下方法查找最终重定向的网址时

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

It will show the url of the immediate page after clicking the link, but NOT the final one. 单击链接后,它将显示立即页面的URL,而不显示最后一个。

Have I missed out anything or is my code insufficient to deal with re-direction of the page after a delay in that re-direction? 我是否错过了任何东西,或者我的代码在延迟重定向后是否不足以处理页面的重定向? I have qneuired about it here, but to no avail and if anyone can point me to the similar issue, I will be most grateful. 我对此有些怀疑,但无济于事,如果有人能指出我类似的问题,我将不胜感激。

It usually happens due to late call to new URL. 通常是由于延迟调用新URL而发生的。 There are various ways to handle this: 有多种方法可以解决此问题:

  • Try waiting for some element on final page, then do getCurrentUrl(). 尝试等待最后一页上的某些元素,然后执行getCurrentUrl()。
  • Another way is to apply while loop till the current URL changes. 另一种方法是应用while循环,直到当前URL更改为止。

I use following code for my tests: 我使用以下代码进行测试:

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

Either way should work. 两种方法都应该起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM