为什么我的测试是抛出异常,无法在webdriver的定位元素? [英] Why my test is throwing Exception-Unable to locate element in webdriver?

查看:354
本文介绍了为什么我的测试是抛出异常,无法在webdriver的定位元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

package testproject;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.*;
  public class mytestclass {
    public static void main(String[] args) {
       WebDriver Driver = new FirefoxDriver();
        Driver.get("https://www.gmail.com/");
         WebElement wb= Driver.findElement(By.name("Email"));
         wb.sendKeys("sweta");
         WebElement wb1= Driver.findElement(By.name("Passwd"));
         wb1.sendKeys("123456");
         WebElement wb2= Driver.findElement(By.id("signIn"));
         wb2.click();
         WebElement wb3= Driver.findElement(By.xpath(".//*[@id='gb']/div[1]/div[1]/div[2]/div[5]/div[1]/a"));
         wb3.click();
         WebElement wb4= Driver.findElement(By.id("gb_71"));
         wb4.click();
   }
}

当我执行此code一切罚款下去,直到我想要被点击按钮,在标志点。我得到的例外它说,
异常线程mainorg.openqa.selenium.NoSuchElementException:找不到元素:{法:的XPath,选择:.//*[@ ID ='GB'] / DIV [1] / DIV [1] / DIV [2] / DIV [5] / DIV [1] / A},但是当我试图用它的fierbug做工精细定位。
在上述code我改变了电子邮件ID和密码,以保持电子邮件的安全。

When i am executing this code everything is going fine till the point where i want the sign in button to be clicked. I am getting exception which says that Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"xpath","selector":".//*[@id='gb']/div[1]/div[1]/div[2]/div[5]/div[1]/a"} but when i am trying to locate it using fierbug its working fine. In the above mentioned code i changed the email id and password to keep the email safe.

我面临多一个程序的问题,我在stakwave已经发布,所以如果ü可以,那么请看看这个链接 - <一个href=\"http://stackoverflow.com/questions/21018649/webdriver-is-not-able-to-click-on-a-hyperlink-in-fierfox\">webdriver不能点击的超链接在fierfox

I was facing problem with one more program which i already posted on stakwave so if u can then please have a look at this link-webdriver is not able to click on a hyperlink in fierfox

推荐答案

在你的XPath前pression开头删除的点。这样,你有一个XPath前pression thaty可以匹配一切。与在开始点你可能retrict自己取决于如果当前节点是根节点与否。疗法EIS没有办法知道。就在事实的点只能给你添麻烦了。不幸的是,你不能总是相信什么工具像萤火虫给你(它仍然是在案件的99%,如此)。

Remove the dot at the beginning of your xpath expression. That way you have an xpath expression thaty could match everything. With the dot at the beginning you might retrict yourself depending on if the current node is the root node or not. Ther eis no way to know it. Just the fact the dot can only give you trouble. Unfortunately you cannot always trust what tools like firebug give you (it is still true in 99% of the case).

当然,请确保您的目标elemetns已经在屏幕上由previous答案的建议。

Of course, ensure that the elemetns you are targeting are already on the screen as suggested by the previous answer.

这篇关于为什么我的测试是抛出异常,无法在webdriver的定位元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆