zl程序教程

您现在的位置是:首页 >  其他

当前栏目

Webdriver定位不到元素的解决办法

定位 元素 解决办法 不到 WebDriver
2023-09-11 14:19:43 时间
不知道怎么回事,先前能跑动的case,现在元素始终找不到。 但是我xpath是能定位得到的,debug了一下,结果发现在 WebElementelement = locator.findElement();就卡住了。 弄了好久也没有成功。 网上找例子: Selenium2(WebDriver)_如何判断WebElement元素对象是否存在 1.selenium中如果去寻找元素,而元素不存在的话,通常会抛出NoSuchElementException 导致测试失败,但有时候,我们需要去确保页面元素不存在,才是我们正确的验收条件下面的方法可以用来判定页面元素是否存在 1 public boolean doesWebElementExist(WebDriver driver, By selector) 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("//a[contains(text(),"+username+")]")); return linkUsername.isDisplayed(); 这一方法的前提是:该元素之前已经存在,仅仅需要判断是否被显示。 现在存在另一种场合,页面元素并不存在,即通过driver.findElement只能在超时之后得到NoSuchElementException的异常。 因此只好通过如下方法解决:
1、这一方法不属于任何一个page页,因此需要额外进行框架上的变更以支持这些功能函数,否则就必须在每一个用到该函数的page类写一遍。 2、仍然需要等到超时才能得知结果,当需要频繁使用该函数的时候会造成相当的时间浪费。 类似于seleniumRC中的isTextPresent方法 用xpath匹配所有元素(//*[contains(.,keyword)]),判断是否存在包含期望关键字的元素。 使用时可以根据需要调整参数和返回值。
1 public boolean isContentAppeared(WebDriver driver,String content) { 2       booleanstatus = false; 3       try { 4          driver.findElement(By.xpath("//*[contains(.," + content +")]")); 5           System.out.println(content + "is appeard!"); 6           status = true; 7       } catch (NoSuchElementException e) { 8           status = false; 9           System.out.println("" +content + " doesnt exist!")); 10      } 11      return status;
3 WebElement PageLink; 4 PageLink = driver.findElement(By.xpath("//a[@class = PageLink and@title ="+Integer.toString(currentPageLinkNumber+1)+"]")); 5 PageLink.click(); 6 currentPageLinkNumber++; 7 //OtherOperation(); 然后又看了一篇文章 selenium webdriver定位不到元素的五种原因及解决办法 1.动态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”后面的数字会随着你每次登陆而变化,此时就无法通过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" !DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"  "http://www.w3.org/TR/html4/loose.dtd" html head body div id="menu_node_type_0" table width="193" cellspacing="0" cellpadding="0" border="0" tbody td id="c_1" table cellspacing="0" cellpadding="0" border="0" align="center" tbody 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("//center/div[@ ]/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."); } 最后看了乙醇的文章
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();
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);
总算成功了,成功来得真不容易啊! 我现在还是不明白我之前的方法为什么开始可以,后面就不行了。 但是我现在也不去探究了,我自己的问题已经解决了。 谢谢分享文章的朋友。 最新内容请见作者的GitHub页:http://qaseven.github.io/