我尝试使用Yahoo搜索搜索方案。但是只搜索了数组中的第一个元素。相同的代码适用于Google搜索。代码如下:
package googleSearch;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
public class YahooSearch {
WebDriver driver;
@BeforeClass
public void setup() {
System.setProperty("webdriver.chrome.driver","D://chromedriver.exe");
driver=new ChromeDriver();
driver.get("http://www.yahoo.com");
}
@AfterClass
public void quit(){
driver.manage().deleteAllCookies();
driver.quit();
}
@DataProvider (name="Searchtext")
public Object [][] data(){
return new Object [][] {{"Teacher"},{"School"}};
}
@Test(dataProvider="Searchtext")
public void search(String text) {
WebElement txtsearch = driver.findElement(By.xpath(".//*[@id='p_13838465-p']"));
txtsearch.sendKeys(text);
WebElement btnSub= driver.findElement(By.xpath(".//*[@id='search-submit']"));
btnSub.submit();
}
}
错误显示如下:
PASSED: search("Teacher")
FAILED: search("School")
org.openqa.selenium.NoSuchElementException: no such element
(Session info: chrome=37.0.2062.120)
(Driver info: chromedriver=2.9.248315,platform=Windows NT 6.1 SP1 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 64 milliseconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.29.0', revision: '58258c3', time: '2013-01-17 22:47:00'
System info: os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.7.0_45'
Session ID: 9df485bf13d7a846a1be5a3d9e0e9570
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities [{platform=XP, acceptSslCerts=true, javascriptEnabled=true, browserName=chrome, chrome={userDataDir=C:\Users\Aloka\AppData\Local\Temp\scoped_dir4652_2250}, rotatable=false, locationContextEnabled=true, version=37.0.2062.120, takesHeapSnapshot=true, cssSelectorsEnabled=true, databaseEnabled=false, handlesAlerts=true, browserConnectionEnabled=false, webStorageEnabled=true, nativeEvents=true, applicationCacheEnabled=false, takesScreenshot=true}]
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:187)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:145)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:533)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:302)
at org.openqa.selenium.remote.RemoteWebDriver.findElementByXPath(RemoteWebDriver.java:387)
at org.openqa.selenium.By$ByXPath.findElement(By.java:344)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:294)
at googleSearch.YahooSearch.search(YahooSearch.java:35)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:714)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
at org.testng.TestRunner.privateRun(TestRunner.java:767)
at org.testng.TestRunner.run(TestRunner.java:617)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
at org.testng.SuiteRunner.run(SuiteRunner.java:240)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)
at org.testng.TestNG.run(TestNG.java:1057)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)
=============================================== 默认测试
答案 0 :(得分:0)
你找不到元素。
使用chrome更好地定位元素 - >打开你的页面 - > check元素 - >右键单击元素 - >复制CSS路径(如果需要,则复制xpath)。 在定位器中使用此css。