qileilove

          blog已經轉移至github,大家請訪問 http://qaseven.github.io/

          Webdriver定位不到元素的解決辦法

           不知道怎么回事,先前能跑動的case,現在元素始終找不到。
            但是我xpath是能定位得到的,debug了一下,結果發現在
            WebElementelement = locator.findElement();就卡住了。
            弄了好久也沒有成功。
            網上找例子:
            Selenium2(WebDriver)_如何判斷WebElement元素對象是否存在
            1.selenium中如果去尋找元素,而元素不存在的話,通常會拋出NoSuchElementException 導致測試失敗,但有時候,我們需要去確保頁面元素不存在,才是我們正確的驗收條件下面的方法可以用來判定頁面元素是否存在
              1 public boolean doesWebElementExist(WebDriver driver, By selector)
              2{
              3
              4         try
              5         {
              6                driver.findElement(selector);
              7                returntrue;
              8        }
              9        catch(NoSuchElementException e)
              10         {
              11                 return false;
              12         }
              13 }
            2.一般有這樣的應用場合,例如我們要驗證在一個網站是否登錄成功,那么可以通過判斷登錄之后是否顯示相應元素:
            WebElementlinkUsername =driver.findElement(By.xpath("http://a[contains(text(),"+username+")]"));
            return linkUsername.isDisplayed();
            這一方法的前提是:該元素之前已經存在,僅僅需要判斷是否被顯示。
            現在存在另一種場合,頁面元素并不存在,即通過driver.findElement只能在超時之后得到NoSuchElementException的異常。
            因此只好通過如下方法解決:
          1 boolean ElementExist (ByLocator )
          2{
          3 try
          4 {
          5 driver.findElement( Locator );
          6 returntrue;
          7}
          8 catch(org.openqa.selenium.NoSuchElementException ex)
          9{
          10     returnfalse;
          11 }
          12 }
            但這一方法仍然不理想,有這樣兩個問題:
            1、這一方法不屬于任何一個page頁,因此需要額外進行框架上的變更以支持這些功能函數,否則就必須在每一個用到該函數的page類寫一遍。
            2、仍然需要等到超時才能得知結果,當需要頻繁使用該函數的時候會造成相當的時間浪費。
            3.
            類似于seleniumRC中的isTextPresent方法
            用xpath匹配所有元素(//*[contains(.,'keyword')]),判斷是否存在包含期望關鍵字的元素。
            使用時可以根據需要調整參數和返回值。
          1 public boolean isContentAppeared(WebDriver driver,String content) {
          2       booleanstatus = false;
          3       try {
          4          driver.findElement(By.xpath("http://*[contains(.,'" + content +"')]"));
          5           System.out.println(content + "is appeard!");
          6           status = true;
          7       } catch (NoSuchElementException e) {
          8           status = false;
          9           System.out.println("'" +content + "' doesn't exist!"));
          10      }
          11      return status;
          12 }
            4. Xpath 多重判斷
            1 while(currentPageLinkNumber<MaxPage)
            2 {
            3 WebElement PageLink;
            4 PageLink = driver.findElement(By.xpath("http://a[@class = 'PageLink' and@title ='"+Integer.toString(currentPageLinkNumber+1)+"']"));
            5 PageLink.click();
            6 currentPageLinkNumber++;
            7 //OtherOperation();
            8 }
            然后又看了一篇文章
            selenium webdriver定位不到元素的五種原因及解決辦法
            1.動態id定位不到元素
            for example:
            //WebElement xiexin_element = driver.findElement(By.id("_mail_component_82_82"));
            WebElement xiexin_element = driver.findElement(By.xpath("http://span[contains(.,'寫  信')]"));
            xiexin_element.click();
            上面一段代碼注釋掉的部分為通過id定位element的,但是此id“_mail_component_82_82”后面的數字會隨著你每次登陸而變化,此時就無法通過id準確定位到element。
            所以推薦使用xpath的相對路徑方法查找到該元素。

           2.iframe原因定位不到元素
            由于需要定位的元素在某一個frame里邊,所以有時通過單獨的id/name/xpath還是定位不到此元素
            比如以下一段xml源文件:
          <iframe id="left_frame" scrolling="auto" frameborder="0" src="index.php?m=Index&a=Menu" name="left_frame" noresize="noresize" style="height: 100%;visibility: inherit; width: 100%;z-index: 1">
          <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"  "http://www.w3.org/TR/html4/loose.dtd">
          <html>
          <head>
          <body class="menuBg">
          <div id="menu_node_type_0">
          <table width="193" cellspacing="0" cellpadding="0" border="0">
          <tbody>
          <tr>
          <tr>
          <td id="c_1">
          <table class="menuSub" cellspacing="0" cellpadding="0" border="0" align="center">
          <tbody>
          <tr class="sub_menu">
          <td>
          <a href="index.php?m=Coupon&a=SearchCouponInfo" target="right_frame">密碼重置</a>
          </td>
          </tr>
            原本可以通過
            WebElement element = driver.findElement(By.linkText("密碼重置"));
            來定位此元素,但是由于該元素在iframe id="left_frame"這個frame里邊  所以需要先通過定位frame然后再定位frame里邊的某一個元素的方法定位此元素
            WebElement element =driver.switchTo().frame("left_frame").findElement(By.linkText("密碼重置"));
            3.不在同一個frame里邊查找元素
            大家可能會遇到頁面左邊一欄屬于left_frame,右側屬于right_frame的情況,此時如果當前處在
            left_frame,就無法通過id定位到right_frame的元素。此時需要通過以下語句切換到默認的content
            driver.switchTo().defaultContent();
            例如當前所在的frame為left_frame
            WebElement xiaoshoumingxi_element = driver.switchTo().frame("left_frame").findElement(By.linkText("銷售明細"));
            xiaoshoumingxi_element.click();
            需要切換到right_frame
            driver.switchTo().defaultContent();
            Select quanzhong_select2 = new Select(driver.switchTo().frame("right_frame").findElement(By.id("coupon_type_str")));
            quanzhong_select2.selectByVisibleText("售后0小時");
            4.  xpath描述錯誤
            這個是因為在描述路徑的時候沒有按照xpath的規則來寫 造成找不到元素的情況出現
            5.點擊速度過快  頁面沒有加載出來就需要點擊頁面上的元素
            這個需要增加一定等待時間,顯示等待時間可以通過WebDriverWait 和util來實現
            例如:
            //用WebDriverWait和until實現顯示等待 等待歡迎頁的圖片出現再進行其他操作
            WebDriverWait wait = (new WebDriverWait(driver,10));
            wait.until(new ExpectedCondition<Boolean>(){
            public Boolean apply(WebDriver d){
            boolean loadcomplete = d.switchTo().frame("right_frame").findElement(By.xpath("http://center/div[@class='welco']/img")).isDisplayed();
            return loadcomplete;
            }
            });
            也可以自己預估時間通過Thread.sleep(5000);//等待5秒 這個是強制線程休息
            6.firefox安全性強,不允許跨域調用出現報錯
            錯誤描述:uncaught exception: [Exception... "Component returned failure code: 0x80004005 (NS_ERROR_FAILURE) [nsIDOMNSHTMLDocument.execCommand]" nsresult: "0x80004005 (NS_ERROR_FAILURE)" location:
            解決辦法:
            這是因為firefox安全性強,不允許跨域調用。
            Firefox 要取消XMLHttpRequest的跨域限制的話,第一
            是從 about:config 里設置 signed.applets.codebase_principal_support = true; (地址欄輸入about:config 即可進行firefox設置)
            第二就是在open的代碼函數前加入類似如下的代碼: try { netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead"); } catch (e) { alert("Permission UniversalBrowserRead denied."); }
            最后看了乙醇的文章
          import java.io.File;
          importorg.openqa.selenium.By;
          importorg.openqa.selenium.WebDriver;
          importorg.openqa.selenium.chrome.ChromeDriver;
          importorg.openqa.selenium.support.ui.ExpectedCondition;
          importorg.openqa.selenium.support.ui.WebDriverWait;
          public classButtonDropdown {
          public static voidmain(String[] args) throws InterruptedException {
          WebDriver dr = newChromeDriver();
          File file = newFile("src/button_dropdown.html");
          String filePath = "file:///" + file.getAbsolutePath();
          System.out.printf("nowaccesss %s \n", filePath);
          dr.get(filePath);
          Thread.sleep(1000);
          //      定位text是watir-webdriver的下拉菜單
          //      首先顯示下拉菜單
          dr.findElement(By.linkText("Info")).click();
          (newWebDriverWait(dr, 10)).until(new ExpectedCondition<Boolean>(){
          public Booleanapply(WebDriver d){
          returnd.findElement(By.className("dropdown-menu")).isDisplayed();
          }
          });
          //      通過ul再層級定位
          dr.findElement(By.className("dropdown-menu")).findElement(By.linkText("watir-webdriver")).click();
          Thread.sleep(1000);
          System.out.println("browser will be close");
          dr.quit();
          }
          }
            然后我自己定位的。
          public XiaoyuanactivityPage zipaixiuye(){
          driver.navigate().refresh();
          luntan.click();
          WebDriverWrapper.waitPageLoad(driver,3);
          (new WebDriverWait(driver, 10)).until(newExpectedCondition<Boolean>() {
          public Boolean apply(WebDriverdriver){
          returndriver.findElement(By.className("TFB_sub_li")).isDisplayed();
          }
          });
          driver.findElement(By.className("TFB_sub_li")).findElement(By.linkText("自拍秀")).click();
          returnPageFactory.initElements(this.getDriver(),
          XiaoyuanactivityPage.class);
          }
            總算成功了,成功來得真不容易啊!
            我現在還是不明白我之前的方法為什么開始可以,后面就不行了。
            但是我現在也不去探究了,我自己的問題已經解決了。
            謝謝分享文章的朋友。

          posted on 2014-12-11 23:30 順其自然EVO 閱讀(16320) 評論(0)  編輯  收藏 所屬分類: 測試學習專欄

          <2014年12月>
          30123456
          78910111213
          14151617181920
          21222324252627
          28293031123
          45678910

          導航

          統計

          常用鏈接

          留言簿(55)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 大邑县| 兰考县| 盱眙县| 吉木萨尔县| 松潘县| 忻城县| 博爱县| 孟村| 武邑县| 淮滨县| 莫力| 华阴市| 玉龙| 西吉县| 房产| 民和| 高邑县| 花莲县| 佛教| 大同县| 炉霍县| 六安市| 原平市| 屯留县| 象州县| 中阳县| 乃东县| 枣庄市| 天气| 江源县| 普安县| 鄂伦春自治旗| 确山县| 兰坪| 朝阳区| 呼和浩特市| 石泉县| 张家川| 寿阳县| 西盟| 吉木乃县|