Java 为什么我的测试抛出异常-无法在 webdriver 中定位元素?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20998375/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why my test is throwing Exception-Unable to locate element in webdriver?
提问by Shantanu Nandan
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();
}
}
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.
当我执行此代码时,一切正常,直到我希望单击登录按钮为止。我收到异常,它说线程“main”org.openqa.selenium.NoSuchElementException 中的异常:无法定位元素:{“方法”:“xpath”,“选择器”:“.//*[@id='gb ']/div[1]/div[1]/div[2]/div[5]/div[1]/a"} 但是当我尝试使用 fierbug 定位它时,它工作正常。在上面提到的代码中,我更改了电子邮件 ID 和密码以确保电子邮件安全。
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 firefox
我已经在 stakwave 上发布了另一个程序,我遇到了问题,所以如果可以,请查看此链接 - webdriver 无法单击 firefox 中的超链接
回答by Charlie
Are you certain your page is completely loaded after you sign in?
您确定您的页面在登录后已完全加载吗?
Did you set a timeout for your webdriver? (how long it has to wait for elements). Probably it reads your html before it's completey loaded.
您是否为您的网络驱动程序设置了超时?(它必须等待元素多长时间)。可能它会在完全加载之前读取您的 html。
Webdriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);
To find out quickly if this is the problem do Thread.sleep(8000)
after you do wb2.click();
要快速找出这是否是问题所在,请Thread.sleep(8000)
在完成之后执行wb2.click();
回答by Algiz
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).
删除 xpath 表达式开头的点。这样你就有了一个可以匹配所有内容的 xpath 表达式。使用开头的点,您可能会根据当前节点是否为根节点来限制自己。没有办法知道它。事实上,点只能给你带来麻烦。不幸的是,您不能总是相信 firebug 之类的工具为您提供的工具(在 99% 的情况下仍然如此)。
Of course, ensure that the elemetns you are targeting are already on the screen as suggested by the previous answer.
当然,请确保您的目标元素已经按照上一个答案的建议出现在屏幕上。
回答by Manvendra Kumar Mishra
remove the dot(.) and star(*) from the xpath and give proper tag name in place of star.
从 xpath 中删除 dot(.) 和 star(*) 并给出适当的标签名称来代替 star。
for example if @id=gb is the id of div element, place div in place of star. Hope it will work.
例如,如果@id=gb 是 div 元素的 id,则将 div 放在 star 的位置。希望它会起作用。
回答by ramkr
I faced similar problem, issue resolved after setting timeout.
我遇到了类似的问题,设置超时后问题解决了。
Webdriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);
Not sure whats the role of timeout here though.
不确定超时在这里的作用是什么。
回答by user8119294
//launch browser
FirefoxDriver driver = new FirefoxDriver(options);
driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);
//gmail login :
driver.get("http://www.gmail.com");
driver.findElement(By.id("identifierId")).sendKeys("****",Keys.ENTER);
Thread.sleep(5000);
driver.findElement(By.id("password")).sendKeys("***",Keys.ENTER);
//logout:
driver.findElement(By.xpath("//div[@id='gb']/div[1]/div[1]/div[2]/div[4]/div[1]/a/span")).click();
Thread.sleep(5000);
driver.findElement(By.linkText("Sign out")).click();