java - 方法 getCurrentUrl();返回不正确的 URL

标签 java selenium selenium-webdriver

其实我才刚刚开始用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);

最佳答案

您必须在单击“使用条款”链接后添加一些明确的等待时间,否则会添加一些延迟。

修改后的代码:

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

关于java - 方法 getCurrentUrl();返回不正确的 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50459607/

相关文章:

java - 从 sqlback 格式的文件中提取数据以转换为 CSV

Java\n 行分隔符不起作用

java - ApplicationClass 类NotFoundException

java - Bean创建异常: Error creating bean with name '/' defined in ServletContext

java - Gradle 无法解析 sikulixapi

python 3.6 selenium webdriver 错误 X display is required for sending-keys unable to ues Xvfb

python - 使用xpath中的Starts with和ends函数查找selenium元素

python - 将 chromedriver 与 Selenium 结合使用时,如何纠正超时错误?

python - 如何使用带有 Python 的 Selenium WebDriver 获取选定的选项?

java - 如何使用其他浏览器运行 selenium?