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

pew*_*007 25 java paging selenium webdriver

我在Java中使用Selenium Webdriver.我想在点击"下一步"按钮后从第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);
    }   
Run Code Online (Sandbox Code Playgroud)

在获取当前url之前,我有隐式和显式等待调用以等待页面完全加载.但是,它仍然打印出第1页的网址(预计它将成为第2页的网址).

rag*_*fin 26

就像你说的那样,因为下一个按钮的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);
} 
Run Code Online (Sandbox Code Playgroud)