zoukankan      html  css  js  c++  java
  • Selenium得到当前页面的URL

        /**
             * getCurrentURL:(get the current page URL address). 
    
             * @author huchan
             * @param driver  --- the web driver instance
             * @return String ---the url of current page
             * @since JDK 1.6
             */
            public String getCurrentPageURL(){
                String pageurl="";
                JavascriptExecutor je=(JavascriptExecutor) driver;
                final String docstate=(String) je.executeScript("return document.readyState");
                logger.info("Current loading page state is:"+docstate);
                WebDriverWait wait=new WebDriverWait(driver,120);
                ExpectedCondition<Boolean> ec = new ExpectedCondition<Boolean>() {
                      @Override
                    public Boolean apply(WebDriver d) {
                        return (docstate.equals("complete"));
                      }
                    };
                try{
                   logger.info("We just wait for the current page load correctly now...");
                   wait.until(ec);           
                   pageurl=driver.getCurrentUrl();
                   logger.info("we found the current url is:"+pageurl);
                }
                catch(TimeoutException e){
                    pageurl="time out:120 seconds";
                    logger.error("Sorry the page cannot be loaded correctly:"+e.getMessage()+driver.getCurrentUrl());
                }
                return pageurl;
            }

    不多说,代码如上。关键点是一定要等待页面加载完成了再使用driver.getCurrentUrl()方法,否则的话可能得到的页面URL 不正确。

  • 相关阅读:
    学习进度7
    《机器学习十讲》学习报告六
    《机器学习十讲》学习报告五
    《机器学习十讲》学习报告四
    《机器学习十讲》学习报告三
    华为机试题 仿苹果
    C++ STL 六大组件的交互关系
    C++ STL 源码 阅读
    抽象类和接口的区别
    重载 & 重写 在java 中
  • 原文地址:https://www.cnblogs.com/alterhu/p/3357420.html
Copyright © 2011-2022 走看看