zoukankan      html  css  js  c++  java
  • WebDriver一些常见问题的解决方法【转】

    转至:http://www.cnblogs.com/sylovezp/p/4329770.html

    1.Exception NoSuchElementException:
      解决方法:
      1)检查目标element的locator
      2)如果locator是正确的,尝试在查找element之前等待页面的加载
      3)如果等待了很久也一直没有找到element,尝试使用另外一个locator
    2.Exception NoSuchWindowException
      解决方法:
      1)检查窗口的locator
      2)在找窗口之前,等到页面的加载
    3.Exception NoAlertPresentException
      解决方法:
      1)确认alert(javascript 顶层的窗口,不是最新的)是当前的
      2)在操作alert之前等待页面的加载
    4.Exception NoSuchFrameException
      解决方法:
      1)检查frame的locator
      2)检查这个frame是否有一些父frame(如果有父frame的话,应该先转换到父frame)
      3)在转换到目标frame之前,确认转换到了默认的content(仅有一个frame)
      4)在转换frame之前等待页面的加载
    5.Exception UnhandledAlertException
      解决方法:
      1)Check if there is some alert dialog present. ( JavaScript pop window). And deal with them.
      2)If no javascript pop window present but the exception still occurs. Make sure the developer ols 
      is closed when running automation case. (Because since selenium 2.19. “UnhandledAlertException” 
      added and they think the developer tool is an alert)
    6.Exception UnexpectedTagNameException
      解决方法:
      1)Check the target element’s html tag name.
      2)Try to wait for page load then initializing the selector.
    7.Exception StaleElementReferenceException
      解决方法:
      1)Re-find the element again. (Because the element has been refresh.)
    8.Exception TimeoutException
      解决方法:
      1)Check the expected conditions locator.
      2)Increase the wait time.

    9.org.openqa.selenium.StaleElementReferenceException: Element not found in the cache - perhaps the page has changed since it was looked up

      因此可以看出,在经过一次点击后,原有PageLink已经失效。需要重新获取。
      其原因在于,点击过一次PageLink后,会重新刷新并生成新PageLink,当前页的PageLink不会显示。
      用Selenium <wbr>WebDriver获取WebElement时的元素过期问题
      因此解决方法是设定了两个参数
      private Integer currentPageLinkNumber = 1;
      private Integer MaxPage = 10;//Max page links number
      然后通过
    复制代码
    while(currentPageLinkNumber<MaxPage)
    {
    WebElement PageLink;
    PageLink = driver.findElement(By.xpath("//a[@class = 'PageLink' and @title ='"+Integer.toString(currentPageLinkNumber+1)+"']"));
    PageLink.click();
    currentPageLinkNumber++;
    //OtherOperation();
    }
    复制代码
      的方式进行迭代。
      虽然感觉很麻烦就是- -
      要很小心的注意同步currentPageLinkNumber和当前的PageLink
    我用的另一种方法来解决过期的问题:
    复制代码
    int i = 1;
            int j = 0;
            while(i!=0){
                List<WebElement> links = driver.findElements(By.xpath("//a[@href]"));
                WebElement link = links.get(j);
                String httpurl = "http://";
                String url = link.getAttribute("href");
                String text = link.getText();
                System.out.println(url+"    "+text);
                if(url.contains(httpurl)){
                    //如果是隐藏的属性的话,就会报错,明天看下如何去掉隐藏元素的干扰  //*[@id='page']/div[2]/div[2]/h1/a
                    if(driver.findElement(By.xpath("//a[@href]")).toString().equals("http://www.1905.com/")){
                        continue;
                    }
                    link.click();
                    navigate.back();
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    j++;
                    if(j>links.size()){
                            break;
                        }
                    }
            }
    复制代码
  • 相关阅读:
    学习使用GitHub托管团队代码开展协作
    实验一 GIT 代码版本管理
    实验五 单元测试
    实验二 结对编程(阶段二)
    结对编程 第一阶段
    实验一 GIT代码版本管理
    实验五 单元测试
    实验二 结对编程(第二阶段)
    结对编程 第一阶段报告
    实验一 GIT代码版本管理
  • 原文地址:https://www.cnblogs.com/keepSmile/p/5168597.html
Copyright © 2011-2022 走看看