問題描述
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();
}
}
當我執行此代碼時,一切都很好,直到我希望單擊登錄按鈕.我得到了例外,它說線程main" org.openqa.selenium.NoSuchElementException 中的異常:無法定位元素:{method":xpath",selector":.//*[@id='gb']/div[1]/div[1]/div[2]/div[5]/div[1]/a"} 但是當我嘗試使用 fierbug 定位它時,它工作正常.在上述代碼中,我更改了電子郵件 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 上發布了另一個程序的問題,所以如果你可以的話,請看看這個鏈接 -webdriver 無法點擊 firefox 中的超鏈接
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
推薦答案
我遇到了類似的問題,設置超時后問題解決.
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.
這篇關于為什么我的測試拋出異常 - 無法在 webdriver 中定位元素?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!