无法使用selenium webdriver https url将文本值发送到文本框

时间:2017-03-20 15:42:08

标签: javascript selenium xpath selenium-webdriver

我无法点击此“用户名”元素:

<tbody>
<tr id="login_box_msgs">
<tr>
<td>
<form id="login-form" class="row form login-form login-form--align-start form--pad-none" style="padding:0">
<fieldset>
<div class="form-field form-field--text form-field--size-medium">
<label class="form-field__label" for="username">
<span class="form-field__contents">
<input id="username" value="" size="26" onkeypress=" return checkEnter(event); " type="text">
</span>
</div>
<div class="form-field form-field--text form-field--size-medium">
</fieldset>

我的代码是:

DesiredCapabilities capability = DesiredCapabilities.chrome();

        capability.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);

        System.setProperty("webdriver.chrome.driver", "C:/Softwares/selenium/chromedriver_win32/chromedriver.exe");

        WebDriver driver = new ChromeDriver(capability);
        driver.get("https://15.146.50.208");
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
        driver.manage().window().maximize(); 
        System.out.println("Page title is: " + driver.getTitle());
        //Thread.sleep(5000);
        WebElement element = driver.findElement(By.xpath(".//*[@id='username']"));

试着用这个:

Actions navigator = new Actions(driver);
    navigator.click(element)
        .sendKeys(Keys.END)
        .keyDown(Keys.SHIFT)
        .sendKeys(Keys.HOME)
        .keyUp(Keys.SHIFT)
        .sendKeys(Keys.BACK_SPACE)
        .sendKeys("Administrator")
        .perform();

和此:

element.click();
        element.sendKeys("Administrator");

和此:

JavascriptExecutor js =(JavascriptExecutor)驱动程序;     js.executeScript( “$( '#用户名')ATTR( '值', '管理员')。”);

和此:

driver.findElement(By.xpath( “.//*[@ ID = '用户名'] ”))的SendKeys(“ 管理员”);

输出:

Starting ChromeDriver 2.28.455520 (cc17746adff54984afff480136733114c6b3704b) on port 35038
Only local connections are allowed.
Mar 21, 2017 12:20:21 AM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
Page title is: iLO: WIN-5NJLS78KH9C - ILOAF6BNP0266
Exception in thread "main" org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":".//*[@id='username']"}
  (Session info: chrome=57.0.2987.110)
  (Driver info: chromedriver=2.28.455520 (cc17746adff54984afff480136733114c6b3704b),platform=Windows NT 10.0.14393 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 30.06 seconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '3.3.1', revision: '5234b32', time: '2017-03-10 09:04:52 -0800'
System info: host: 'WINDOWS-CPRD7P5', ip: '15.146.51.224', os.name: 'Windows NT (unknown)', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_121'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities [{applicationCacheEnabled=false, rotatable=false, mobileEmulationEnabled=false, networkConnectionEnabled=false, chrome={chromedriverVersion=2.28.455520 (cc17746adff54984afff480136733114c6b3704b), userDataDir=C:\Users\ADMINI~1\AppData\Local\Temp\scoped_dir7912_13587}, takesHeapSnapshot=true, pageLoadStrategy=normal, databaseEnabled=false, handlesAlerts=true, hasTouchScreen=false, version=57.0.2987.110, platform=XP, browserConnectionEnabled=false, nativeEvents=true, acceptSslCerts=true, locationContextEnabled=true, webStorageEnabled=true, browserName=chrome, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true, unexpectedAlertBehaviour=}]
Session ID: 0d48e5d489bb3ff29a3459b37b4793fb
*** Element info: {Using=xpath, value=.//*[@id='username']}
    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:216)
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:168)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:638)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:371)
    at org.openqa.selenium.remote.RemoteWebDriver.findElementByXPath(RemoteWebDriver.java:476)
    at org.openqa.selenium.By$ByXPath.findElement(By.java:361)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:363)
    at test1.main(test1.java:33)

请帮忙。

0 个答案:

没有答案