1. 程式人生 > >org.openqa.selenium.NoSuchElementException: Unable to locate element: 異常解決方法

org.openqa.selenium.NoSuchElementException: Unable to locate element: 異常解決方法

現象:出現報錯org.openqa.selenium.NoSuchElementException: Unable to locate element

原因:進入網頁時,網頁可能在loading中,因此找不到元素。

解決方法:你可以讓他休眠Thread.sleep(2000),即休眠2秒,應該可以找到元素了。

Thread.sleep(2000)加在

         System.setProperty("webdriver.firefox.bin","D:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe"); 
        WebDriver driver = new FirefoxDriver();


driver.get("http://localhost:9090/jenkins");
Thread.sleep(2000);   
driver.findElement(By.id("j_username")).sendKeys("admin");
driver.findElement(By.name("j_password")).sendKeys("b7588402d6ee48f5847ff6f7491c3dd2");
driver.findElement(By.id("yui-gen1-button")).click();