Selenium WebDriver:无法选择元素

时间:2016-07-01 15:58:04

标签: selenium xpath selenium-webdriver automated-tests

<input type="text" id="mobile" name="mobile" placeholder="Mobile Number" maxlength="10" value="" onkeyup="javascript:dispLocMob(this);" onkeydown="javascript:dispLocMob(this);" onchange="javascript:dispLocMob(this);">

上面显示的是我正在尝试发送密钥的元素。我尝试过xpath和id以及各种选择器,但它会抛出一个错误,如下所示:

Unable to locate element: {"method":"xpath","selector":"/html/body/div[3]/div/form/div[2]/div[1]/input"}
Command duration or timeout: 338 milliseconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.53.0', revision: '35ae25b', time: '2016-03-15 16:57:40'
System info: host: 'ClaimsCM8', ip: '192.168.110.118', os.name: 'Windows 8', os.arch: 'x86', os.version: '6.2', java.version: '1.7.0_51'
*** Element info: {Using=xpath, value=/html/body/div[3]/div/form/div[2]/div[1]/input}
Session ID: ec543fff-7116-4880-8c98-7c60a1c697d0
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{platform=WINDOWS, acceptSslCerts=true, javascriptEnabled=true, cssSelectorsEnabled=true, databaseEnabled=true, browserName=firefox, handlesAlerts=true, nativeEvents=false, webStorageEnabled=true, rotatable=false, locationContextEnabled=true, applicationCacheEnabled=true, takesScreenshot=true, version=45.0.2}]
    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:206)
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:158)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:678)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:363)
    at org.openqa.selenium.remote.RemoteWebDriver.findElementByXPath(RemoteWebDriver.java:500)
    at org.openqa.selenium.By$ByXPath.findElement(By.java:361)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:355)
    at Selenium.Test2.main(Test2.java:62)
Caused by: org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"xpath","selector":"/html/body/div[3]/div/form/div[2]/div[1]/input"}
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.53.0', revision: '35ae25b', time: '2016-03-15 16:57:40'
System info: host: 'ClaimsCM8', ip: '192.168.110.118', os.name: 'Windows 8', os.arch: 'x86', os.version: '6.2', java.version: '1.7.0_51'
Driver info: driver.version: unknown
    at <anonymous class>.FirefoxDriver.prototype.findElementInternal_(file:///C:/Users/EFERNA~1/AppData/Local/Temp/anonymous4369679942726534324webdriver-profile/extensions/fxdriver@googlecode.com/components/driver-component.js:10770)
    at <anonymous class>.FirefoxDriver.prototype.findElement(file:///C:/Users/EFERNA~1/AppData/Local/Temp/anonymous4369679942726534324webdriver-profile/extensions/fxdriver@googlecode.com/components/driver-component.js:10779)
    at <anonymous class>.DelayedCommand.prototype.executeInternal_/h(file:///C:/Users/EFERNA~1/AppData/Local/Temp/anonymous4369679942726534324webdriver-profile/extensions/fxdriver@googlecode.com/components/command-processor.js:12661)
    at <anonymous class>.DelayedCommand.prototype.executeInternal_(file:///C:/Users/EFERNA~1/AppData/Local/Temp/anonymous4369679942726534324webdriver-profile/extensions/fxdriver@googlecode.com/components/command-processor.js:12666)
    at <anonymous class>.DelayedCommand.prototype.execute/<(file:///C:/Users/EFERNA~1/AppData/Local/Temp/anonymous4369679942726534324webdriver-profile/extensions/fxdriver@googlecode.com/components/command-processor.js:12608)

Web元素中是否有定义的内容不允许我访问Selenium脚本中的元素? 任何建议/意见将受到高度赞赏。 谢谢你!

3 个答案:

答案 0 :(得分:0)

尝试如下: -

WebDriverWait wait = new WebDriverWait(driver, 10);
el = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("mobile")));
el.sendKeys("your value");

注意: - 在finding element之前确保它不在frameiframe内。如果它位于任何frameiframe内,您需要先将frame切换为: - driver.switchTo().frame("frame name or id")

希望它会帮助你...... :)

答案 1 :(得分:0)

首先,如果您使用的是xpath,则使用相对xpath而不是绝对值,因为DOM中的轻微更改会使绝对xpath无效或引用错误的元素。

第二次尝试使用.click();在.sendkeys();之前。所以你的代码可以是

WebElement ele = dvr.findElement(By.id("mobile"));
ele.click();
ele.sendKeys("your string");

最后,请确保您在页面上没有任何具有相同属性的重复元素,即id ==&#34; mobile&#34;。希望这有帮助

答案 2 :(得分:0)

请尝试以下代码:

  1. 如果元素不在任何框架中:

    driver.findElement(By.id("mobile")).sendKeys("");

  2. 如果元素在框架内:

    // Switching to the frame

    driver.switchTo().frame(<framename>);

    driver.findElement(By.id("mobile")).sendKeys("");

    driver.switchTo().defaultContent();

  3. 另外,请在启动页面并输入文本后添加一些等待。即使代码不起作用,请使用以下代码检查元素是否可见:

    if(driver.findElement(By.id("mobile")).isDisplayed()) {
       // Add the code given above
    }
    

    希望这有帮助