方法 getCurrentUrl(); 返回错误的URL。

3

我刚开始学习使用Selenium进行Java编程,但是我无法获取当前的URL。Selenium只返回了起始URL而不是当前URL。

看起来implicitlywait()方法没有起作用,或者我做错了什么其他的事情?

package SeleniumPackage;

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.util.concurrent.TimeUnit;

public class SeleniumClass {

public static void main(String[] args) {

WebDriver driver = new ChromeDriver();

driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

driver.get("https://learn.letskodeit.com/p/practice");

WebElement benzRadioBtn = driver.findElement(By.id("benzradio"));
benzRadioBtn.click();

WebElement hondaRadioBtn = driver.findElement(By.id("hondaradio"));
hondaRadioBtn.click();
WebElement bmwRadioBtn = driver.findElement(By.id("bmwradio"));
bmwRadioBtn.click();
WebElement benzCheckBox = driver.findElement(By.id("benzcheck"));
benzCheckBox.click();
Select dropdown = new Select (driver.findElement(By.id("carselect")));
dropdown.selectByValue("honda");
WebElement element = driver.findElement(By.id("product"));
System.out.println(element.getText());

driver.findElement(By.linkText("Terms of Use")).click();

String currentURL = driver.getCurrentUrl();  
System.out.println(currentURL);

1
可能是使用Selenium WebDriver获取当前页面的URL的重复问题。 - user7294900
4个回答

1

在点击“使用条款”链接后,您需要添加一些显式等待时间,否则请添加一些延迟。

修改后的代码:

driver.findElement(By.linkText("Terms of Use")).click();
//Added Newly
WebDriverWait wait = new WebDriverWait(driver, 15);
wait.until(ExpectedConditions.titleContains("Terms"));

String currentURL = driver.getCurrentUrl();

这也是正确的,但我们也可以使用验证点:P - iamsankalp89
是的。但有时候,页面标题功能也无法正确地给出当前页面的标题。因此,在页面加载操作之后添加一些等待时间总是更好的选择,然后我们可以添加验证。 - Subburaj

1
这是一种预期行为,因为您点击的是iframe的“使用条款”,它不会重定向您的URL(只会更改iframe的URL)。

enter image description here

我建议使用XPath查找所需的元素,如下所示:

WebElement element = driver.findElement(By.xpath("/html[@class=' video no-videoautoplay']/body/div[@class='view-school']/footer[@class='bottom-menu bottom-menu-inverse']/div[@class='container']/div[@class='row']/div[@class='col-xs-12 col-sm-4 col-md-4 footer-column'][2]/ul[@class='list-unstyled']/li[1]/a"));

你是怎么得到这个Xpath的?谢谢。 - SlavaCa
通过Chrome扩展程序,我此时无法记起它的名称。 - Rcordoval

0

如果标题匹配,则可以与标题相匹配,然后打印当前URL,它始终会给出正确的URL。

public static void main(String[] args) throws InterruptedException {
System.out.println("Ready to launch the browser");
System.setProperty("webdriver.chrome.driver", "C:/Users/sankalp.gupta/Desktop/JAVASELN/chromedriver.exe");

WebDriver driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.manage().window().maximize();
driver.get("https://learn.letskodeit.com/p/practice");

WebElement benzRadioBtn = driver.findElement(By.id("benzradio"));
benzRadioBtn.click();

WebElement hondaRadioBtn = driver.findElement(By.id("hondaradio"));
hondaRadioBtn.click();
WebElement bmwRadioBtn = driver.findElement(By.id("bmwradio"));
bmwRadioBtn.click();
WebElement benzCheckBox = driver.findElement(By.id("benzcheck"));
benzCheckBox.click();
Select dropdown = new Select (driver.findElement(By.id("carselect")));
dropdown.selectByValue("honda");
WebElement element = driver.findElement(By.id("product"));
System.out.println(element.getText());

driver.findElement(By.linkText("Terms of Use")).click();
if(driver.getTitle().contains("Practice | Let's Kode It"))
    {
        System.out.println(driver.getCurrentUrl());

    }
}

这个方法没有给我想要的结果。谢谢。 - SlavaCa
你想要什么结果? - iamsankalp89
只需获取更改 iframe 后的当前 URL。 - SlavaCa

0

由于您在上一行中调用了click(),因此接下来要检索URL,您需要结合WebDriverWaitExpectedConditions方法urlContains(java.lang.String fraction),如下所示:

new WebDriverWait(driver, 20).until(ExpectedConditions.urlContains("partial_text_of_url"));
System.out.println(driver.getCurrentUrl());

注意:您需要删除implicitlywait(),因为根据文档

不要混合使用隐式等待和显式等待。这样做可能会导致等待时间不可预测。例如,将隐式等待设置为10秒并将显式等待设置为15秒,可能会在20秒后超时。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接