简体   繁体   English

加载页面后,selenium获取当前URL

[英]selenium get current url after loading a page

I'm using Selenium Webdriver in Java. 我在Java中使用Selenium Webdriver。 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: 我想在点击“下一步”按钮后从第1页到第2页获取当前网址。这是我的代码:

    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. 在获取当前url之前,我有隐式和显式等待调用以等待页面完全加载。 However, it's still printing out the url for page 1 (it's expected to be the url for page 2). 但是,它仍然打印出第1页的网址(预计它将成为第2页的网址)。

Like you said since the xpath for the next button is the same on every page it won't work. 就像你说的那样,因为下一个按钮的xpath在每个页面上是相同的,所以它不起作用。 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. 为什么不使用url更改的事实,因为从您的代码开始,单击下一个按钮时它会更改。 I do C# but I guess in Java it would be something like: 我做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);
} 

Page 2 is in a new tab/window ? 第2页是在新的标签页/窗口中? If it's this, use the code bellow : 如果是这样,请使用以下代码:

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

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: 所以你可以这样做:

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? 你确定selenium实际导航到预期的页面吗? (That may sound like a silly question but are you actually watching the pages change... selenium can be run remotely you know...) (这可能听起来像一个愚蠢的问题,但你实际上是在看页面改变...硒可以远程运行,你知道......)

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

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