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