获得后Selenium getCurrentUrl不会改变

时间:2015-11-19 13:56:24

标签: java selenium selenium-webdriver webdriver

有时在致电driver.get(feedUrl)后,driver.getCurrentUrl()会返回上一个网址。这是为什么?

代码提取:

while((feedItem = sharedQueue.poll()) != null){
    final String feedUrl = feedItem.getLink();

    logger.info("Driver getting " + feedUrl);
    driver.get(feedUrl);

    final String url = driver.getCurrentUrl();
    logger.info("Driver got feedUrl: " + feedUrl + " from driverUrl: " + url );
}

日志提取:

12:59:56 [Scrapper 7] INFO  ArticleScrapper - Driver getting http://www.walesonline.co.uk/news/local-news/world-war-two-evacuee-re-10455706
13:03:59 [Scrapper 7] INFO  ArticleScrapper - Driver got feedUrl: http://www.walesonline.co.uk/news/local-news/world-war-two-evacuee-re-10455706 from driverUrl: http://www.walesonline.co.uk/news/local-news/world-war-two-evacuee-re-10455706
13:03:59 [Scrapper 7] INFO  ArticleScrapper - Driver getting http://www.southwalesargus.co.uk/news/14040777.Delays_to_Newport_s_Welsh_school_could_mean_new_pupils_taught_at_primary/?ref=rss
13:04:42 [Scrapper 7] INFO  ArticleScrapper - Driver got feedUrl: http://www.southwalesargus.co.uk/news/14040777.Delays_to_Newport_s_Welsh_school_could_mean_new_pupils_taught_at_primary/?ref=rss from driverUrl: http://www.walesonline.co.uk/news/local-news/world-war-two-evacuee-re-10455706

如您所见,最后一个日志条目具有正确的feedUrl,但是之前的driverUrl

2 个答案:

答案 0 :(得分:0)

要等待页面加载,您可以明确等待JavascriptExecutor事务执行,

((JavascriptExecutor) myDriver).executeScript("return document.readyState").equals("complete")

答案 1 :(得分:0)

您需要等待driver.get完成加载。您可以使用不同的等待技术 - 称为隐式和显式等待。

见这里:http://www.seleniumhq.org/docs/04_webdriver_advanced.jsp

我个人会使用显式等待:

self.tree.tag_bind(childID, '<Double-1>', jclass.tree_func)