您的位置:軟件測試 > 開源軟件測試 > 開源功能測試工具 > Selenium
selenium webdriver定位不到元素
作者:網(wǎng)絡(luò)轉(zhuǎn)載 發(fā)布時間:[ 2016/12/5 16:54:36 ] 推薦標(biāo)簽:Selenium 功能測試

  1.動態(tài)id定位不到元素
  for example:
  //WebElement  xiexin_element = driver.findElement(By.id("_mail_component_82_82"));
  WebElement xiexin_element = driver.findElement(By.xpath("//span[contains(.,'寫  信')]"));
  xiexin_element.click();
  上面一段代碼注釋掉的部分為通過id定位element的,但是此id“_mail_component_82_82”后面的數(shù)字會隨著你每次登陸而變化,此時無法通過id準(zhǔn)確定位到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: ;visibility: inherit; width:  ;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,右側(cè)屬于right_frame的情況,此時如果當(dāng)前處在
  left_frame,無法通過id定位到right_frame的元素。此時需要通過以下語句切換到默認的content
  driver.switchTo().defaultContent();
  例如當(dāng)前所在的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的規(guī)則來寫 造成找不到元素的情況出現(xiàn)
  5.點擊速度過快  頁面沒有加載出來需要點擊頁面上的元素
  這個需要增加一定等待時間,顯示等待時間可以通過WebDriverWait  和util來實現(xiàn)
  例如:
  //用WebDriverWait和until實現(xiàn)顯示等待  等待歡迎頁的圖片出現(xiàn)再進行其他操作
  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("//center/div[@class='welco']/img")).isDisplayed();
  return loadcomplete;
  }
  });
  也可以自己預(yù)估時間通過Thread.sleep(5000);//等待5秒 這個是強制線程休息
  6.firefox安全性強,不允許跨域調(diào)用出現(xiàn)報錯
  錯誤描述:uncaught exception:  [Exception... "Component returned failure code: 0x80004005 (NS_ERROR_FAILURE)  [nsIDOMNSHTMLDocument.execCommand]" nsresult: "0x80004005 (NS_ERROR_FAILURE)"  location:
  解決辦法:
  這是因為firefox安全性強,不允許跨域調(diào)用。
  Firefox  要取消XMLHttpRequest的跨域限制的話,第一
  是從 about:config 里設(shè)置  signed.applets.codebase_principal_support = true; (地址欄輸入about:config  即可進行firefox設(shè)置)
  第二是在open的代碼函數(shù)前加入類似如下的代碼: try {  netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead"); }  catch (e) { alert("Permission UniversalBrowserRead denied."); }

軟件測試工具 | 聯(lián)系我們 | 投訴建議 | 誠聘英才 | 申請使用列表 | 網(wǎng)站地圖
滬ICP備07036474 2003-2017 版權(quán)所有 上海澤眾軟件科技有限公司 Shanghai ZeZhong Software Co.,Ltd