无法找到搜索框

时间:2017-04-28 07:26:53

标签: java selenium webdriver

我正在尝试为网站编写selenium代码。使用java和testNG框架。我想在搜索框中传递文本,但无法执行此操作,发生了一些错误。

这是我的代码

public class Sele_test {
  @Test
  public void openUrl() throws InterruptedException {
      System.setProperty("webdriver.gecko.driver", "E:\\tools\\geckodriver-v0.15.0-win64\\geckodriver.exe");
      FirefoxDriver driver = new FirefoxDriver();

      driver.get("http://www.testingexcellence.com/");

     driver.findElement(By.xpath(".//*[@placeholder='Search this website …' and @name='s']")).sendKeys("ss");
  }

}

HTML:

<input itemprop="query-input" name="s" placeholder="Search this website …" type="search"><input value="Search" type="submit">

我得到的错误:

FAILED: openUrl
org.openqa.selenium.InvalidArgumentException: Expected [object Undefined] undefined to be a string
Build info: version: 'unknown', revision: '5234b32', time: '2017-03-10 09:00:17 -0800'
System info: host: 'ADMIN', ip: '192.168.5.164', os.name: 'Windows 8.1', os.arch: 'amd64', os.version: '6.3', java.version: '1.8.0_92'
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{moz:profile=C:\Users\admin2\AppData\Local\Temp\rust_mozprofile.kjmLYGRcFu7v, rotatable=false, timeouts={implicit=0, pageLoad=300000, script=30000}, pageLoadStrategy=normal, platform=ANY, specificationLevel=0, moz:accessibilityChecks=false, acceptInsecureCerts=false, browserVersion=53.0, platformVersion=6.3, moz:processID=5584, browserName=firefox, platformName=windows_nt}]
Session ID: 1c4dcc01-dabb-40ed-892d-c9ab969ba11e
    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.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:133)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:99)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:43)
    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:163)
    at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:82)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:604)
    at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:274)
    at org.openqa.selenium.remote.RemoteWebElement.sendKeys(RemoteWebElement.java:98)
    at java_class.Sele_test.openUrl(Sele_test.java:15)
    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:108)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:661)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:869)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1193)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:126)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
    at org.testng.TestRunner.privateRun(TestRunner.java:744)
    at org.testng.TestRunner.run(TestRunner.java:602)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:380)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:375)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:340)
    at org.testng.SuiteRunner.run(SuiteRunner.java:289)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1301)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1226)
    at org.testng.TestNG.runSuites(TestNG.java:1144)
    at org.testng.TestNG.run(TestNG.java:1115)
    at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:132)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:230)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:76)


===============================================
    Default test
    Tests run: 1, Failures: 1, Skips: 0
===============================================


===============================================
Default suite
Total tests run: 1, Failures: 1, Skips: 0
===============================================

2 个答案:

答案 0 :(得分:0)

尝试将此xpath用于搜索框

driver.findElement(By.xpath( “//输入[@名称= 's' 的]”))的SendKeys( “SS”);

它肯定会起作用!! !!

答案 1 :(得分:-1)

我认为你为xpath挑选的元素属性有点棘手(可能太长而且只显示点)。其他一切看起来都不错。请尝试使用此Xpath并让我知道它是如何进行的(注意我在开始时稍微改变了你的xpath ):

driver.findElement(By.xpath("//input[@type='search' and @name='s']")).sendKeys("ss");

祝你好运!