selenium-java-Firefox 特殊異常:介面找不到元素

一小平民發表於2014-10-16

在selenium + java + Firefox 的時候會出現不能通過 By.id()的方式找到介面元素,但是實際上介面上通過fireBug能夠找到該元素的且唯一,為什麼找不到到呢?

原因分析如下:

1.介面中不只存在一個該name或者class或者的名稱的元素;

2.介面中存在iframe框架存在;

3.通過debug模式能夠找到介面元素,但是run時,就找不到元素,唯一的一種解釋:開啟firefox,輸入地址後,介面一直處於載入中,當介面未載入完成,便通過webdriver去中介面元素,當然會出現找不到的情況;解決方案:加入等待處理(等待處理方式),再做查詢元素處理;thread.sleep();

調整前程式碼如下

package info.www.itest;

import static org.junit.Assert.*;

import org.junit.*;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;


public class CreatePost {

	@Before
	public void setUp() throws Exception{
		
	}
	@After
	public void tearDown() throws Exception{
		
	}
	@Test
	public void testLogin(){
		System.setProperty("webdriver.firefox.bin", "D:\\Program Files\\Mozilla Firefox\\firefox.exe");
		WebDriver dr = new FirefoxDriver();
		dr.get("http://www.1988.com/wordpress/wp-login.php");
		//個人地址
		
		dr.findElement(By.id("user_login")).sendKeys("admin");
		
		dr.findElement(By.id("user_pass")).sendKeys("admin");
		dr.findElement(By.id("wp-submit")).click();
		assertTrue(dr.getCurrentUrl().contains("admin"));
	}
}

錯誤資訊如下
org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"id","selector":"user_login"}
Command duration or timeout: 29.28 seconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.41.0', revision: '3192d8a', time: '2014-03-27 17:18:15'
System info: host: 'HS-Q0TFDE785GM3', ip: '169.254.183.27', os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.6.0_16'
Session ID: c438cb88-3d6c-422d-8aca-a95f2d84bfff
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{platform=XP, acceptSslCerts=true, javascriptEnabled=true, browserName=firefox, rotatable=false, locationContextEnabled=true, version=4.0.1, cssSelectorsEnabled=true, databaseEnabled=true, handlesAlerts=true, nativeEvents=false, webStorageEnabled=true, applicationCacheEnabled=true, takesScreenshot=true}]
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
	at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
	at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:193)
	at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:145)
	at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:595)
	at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:348)
	at org.openqa.selenium.remote.RemoteWebDriver.findElementById(RemoteWebDriver.java:389)
	at org.openqa.selenium.By$ById.findElement(By.java:214)
	at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:340)
	at info.www.itest.CreatePost.testLogin(CreatePost.java:27)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
	at java.lang.reflect.Method.invoke(Method.java:597)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
	at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
	at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:236)

調整後程式碼如下
package info.www.itest;

import static org.junit.Assert.*;

import org.junit.*;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;


public class CreatePost {

	@Before
	public void setUp() throws Exception{
		
	}
	@After
	public void tearDown() throws Exception{
		
	}
	@Test
	public void testLogin() throws InterruptedException{
		System.setProperty("webdriver.firefox.bin", "D:\\Program Files\\Mozilla Firefox\\firefox.exe");
		WebDriver driver = new FirefoxDriver();
		driver.get("http://localhost/wordpress/wp-login.php");
		
		System.out.println(driver.getCurrentUrl());
		Thread.sleep(30000);
		WebElement elementName = driver.findElement(By.id("user_login"));
		elementName.sendKeys("admin");
		
		WebElement elementPassword = driver.findElement(By.id("user_pass"));
		elementPassword.sendKeys("admin");
		
		driver.findElement(By.id("wp-submit")).click();
		assertTrue(driver.getCurrentUrl().contains("admin"));
	}
}

執行結果如圖所示

 



相關文章