Java 加载页面后硒获取当前网址

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16242340/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 05:58:35  来源:igfitidea点击:

selenium get current url after loading a page

javaseleniumwebdriverpaging

提问by pew007

I'm using Selenium Webdriver in Java. I want to get the current url after clicking the "next" button to move from page 1 to page 2. Here's the code I have:

我在 Java 中使用 Selenium Webdriver。单击“下一步”按钮从第 1 页移动到第 2 页后,我想获取当前 url。这是我的代码:

    WebDriver driver = new FirefoxDriver();
    String startURL = //a starting url;
    String currentURL = null;
    WebDriverWait wait = new WebDriverWait(driver, 10);

    foo(driver,startURL);

    /* go to next page */
    if(driver.findElement(By.xpath("//*[@id='someID']")).isDisplayed()){
        driver.findElement(By.xpath("//*[@id='someID']")).click();  
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id='someID']")));
        currentURL = driver.getCurrentUrl();
        System.out.println(currentURL);
    }   

I have both the implicit and explicit wait calls to wait for the page to be fully loaded before I get the current url. However, it's still printing out the url for page 1 (it's expected to be the url for page 2).

在获取当前 url 之前,我有隐式和显式等待调用来等待页面完全加载。但是,它仍然打印出第 1 页的 url(它应该是第 2 页的 url)。

采纳答案by ragamufin

Like you said since the xpath for the next button is the same on every page it won't work. It's working as coded in that it does wait for the element to be displayed but since it's already displayed then the implicit wait doesn't apply because it doesn't need to wait at all. Why don't you use the fact that the url changes since from your code it appears to change when the next button is clicked. I do C# but I guess in Java it would be something like:

就像你说的,因为下一个按钮的 xpath 在每个页面上都是一样的,所以它不起作用。它按照编码工作,因为它确实等待显示元素,但由于它已经显示,因此隐式等待不适用,因为它根本不需要等待。为什么不使用 url 更改的事实,因为从您的代码中它似乎在单击下一个按钮时发生更改。我做 C#,但我想在 Java 中它会是这样的:

WebDriver driver = new FirefoxDriver();
String startURL = //a starting url;
String currentURL = null;
WebDriverWait wait = new WebDriverWait(driver, 10);

foo(driver,startURL);

/* go to next page */
if(driver.findElement(By.xpath("//*[@id='someID']")).isDisplayed()){
    String previousURL = driver.getCurrentUrl();
    driver.findElement(By.xpath("//*[@id='someID']")).click();  
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

    ExpectedCondition e = new ExpectedCondition<Boolean>() {
          public Boolean apply(WebDriver d) {
            return (d.getCurrentUrl() != previousURL);
          }
        };

    wait.until(e);
    currentURL = driver.getCurrentUrl();
    System.out.println(currentURL);
} 

回答by KyleM

It's been a little while since I coded with selenium, but your code looks ok to me. One thing to note is that if the element is not found, but the timeout is passed, I think the code will continue to execute. So you can do something like this:

我用 selenium 编码已经有一段时间了,但你的代码对我来说看起来不错。需要注意的一点是,如果没有找到元素,但是超时了,我认为代码会继续执行。所以你可以做这样的事情:

boolean exists = driver.findElements(By.xpath("//*[@id='someID']")).size() != 0

What does the above boolean return? And are you sure selenium actually navigates to the expected page? (That may sound like a silly question but are you actually watching the pages change... selenium can be run remotely you know...)

上面的布尔值返回什么?你确定 selenium 真的导航到预期的页面吗?(这可能听起来像一个愚蠢的问题,但你真的在看着页面变化......硒可以远程运行你知道......)

回答by kev85270

Page 2 is in a new tab/window ? If it's this, use the code bellow :

第 2 页在新选项卡/窗口中?如果是这样,请使用以下代码:

try {

    String winHandleBefore = driver.getWindowHandle();

    for(String winHandle : driver.getWindowHandles()){
        driver.switchTo().window(winHandle);
        String act = driver.getCurrentUrl();
    }
    }catch(Exception e){
   System.out.println("fail");
    }