Webdriver无法在子类下找到元素

时间:2015-04-10 08:35:55

标签: selenium nosuchelementexception

我正在尝试查找元素“core_text savable core_valid_regex”,但每次我收到一些或其他错误。

<form id="core_order_holder" onsubmit="return false;">
<table class="order_holder">
<tbody class="core_loader_holder">
<tr>
<td class="core_item_label">Personal number:</td>
<td class="core_item_content">
<span class="savable-wrapper">
<input class="**core_text savable core_valid_regex**" type="text" name="loginid" size="13" style="outline: medium none;">
<span class="savable">
</span>
</td>
</tr> 

直到现在我尝试了以下方法:

  1. WebElement PersonalNumber = (new WebDriverWait(driver, 30)) .until(ExpectedConditions.presenceOfElementLocated(By.className("order_holder"))).findElement(By.className("core_loader_holder")).findElement(By.className("core_item_content")).findElement(By.className("savable-wrapper")).findElement(By.className("core_text savable"));

  2. WebElement E1 = PersonalNumber.findElement(By.className("core_loader_holder")).findElement(By.className("core_item_content"));

  3. WebElement E2 = E1.findElement(By.className("core_item_content")); WebElement E3 = E2.findElement(By.className("savable-wrapper")); WebElement E4 = E3.findElement(By.className("core_text savable")); WebElement PersonalNumber = driver.findElement(By.className("core_text savable"));

  4. WebElement PersonalNumber = (new WebDriverWait(driver, 30)) .until(ExpectedConditions.presenceOfElementLocated(By.className("core_text savable")));

  5. WebElement E1 = (new WebDriverWait(driver, 30)).until(ExpectedConditions.presenceOfAllElementsLocatedBy(PersonalNumber.findElement(By.className("core_loader_holder"))));

  6. WebElement PersonalNumber = driver.findElement(By.className("order_holder") ).findElement(By.className("core_loader_holder")).findElement(By.className("core_item_content")).findElement(By.className("savable-wrapper")).findElement(By.name("loginid"));

  7. 请在下面找到错误:

    Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"class name","selector":"core_item_content"}
    Command duration or timeout: 82 milliseconds
    For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
    Build info: version: '2.45.0', revision: '32a636c', time: '2015-03-05 22:01:35'
    System info: host: 'PC239657', ip: '172.31.204.104', os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.7.0'
    *** Element info: {Using=class name, value=core_item_content}
    Session ID: 1a8604de-4ec7-4a77-ba8b-b1590ae49ce6
    Driver info: org.openqa.selenium.firefox.FirefoxDriver
    Capabilities [{platform=WINDOWS, acceptSslCerts=true, javascriptEnabled=true, browserName=firefox, rotatable=false, locationContextEnabled=true, version=34.0.5, cssSelectorsEnabled=true, databaseEnabled=true, handlesAlerts=true, nativeEvents=true, webStorageEnabled=true, applicationCacheEnabled=true, 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:204)
        at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:156)
        at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:599)
        at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:268)
        at org.openqa.selenium.remote.RemoteWebElement.findElement(RemoteWebElement.java:171)
        at org.openqa.selenium.remote.RemoteWebElement.findElementByClassName(RemoteWebElement.java:228)
        at org.openqa.selenium.By$ByClassName.findElement(By.java:387)
        at org.openqa.selenium.remote.RemoteWebElement.findElement(RemoteWebElement.java:167)
        at Submit_Order.TrustlySubmitOrder(Submit_Order.java:237)
        at Submit_Order.main(Submit_Order.java:260)
    Caused by: org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"class name","selector":"core_item_content"}
    For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
    Build info: version: '2.45.0', revision: '32a636c', time: '2015-03-05 22:01:35'
    System info: host: 'PC239657', ip: '172.31.204.104', os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.7.0'
    Driver info: driver.version: unknown
        at <anonymous class>.FirefoxDriver.prototype.findElementInternal_(file:///C:/Users/291578/AppData/Local/Temp/anonymous5858848057389913831webdriver-profile/extensions/fxdriver@googlecode.com/components/driver-component.js:10271)
        at <anonymous class>.FirefoxDriver.prototype.findChildElement(file:///C:/Users/291578/AppData/Local/Temp/anonymous5858848057389913831webdriver-profile/extensions/fxdriver@googlecode.com/components/driver-component.js:10283)
        at <anonymous class>.DelayedCommand.prototype.executeInternal_/h(file:///C:/Users/291578/AppData/Local/Temp/anonymous5858848057389913831webdriver-profile/extensions/fxdriver@googlecode.com/components/command-processor.js:12274)
        at <anonymous class>.DelayedCommand.prototype.executeInternal_(file:///C:/Users/291578/AppData/Local/Temp/anonymous5858848057389913831webdriver-profile/extensions/fxdriver@googlecode.com/components/command-processor.js:12279)
        at <anonymous class>.DelayedCommand.prototype.execute/<(file:///C:/Users/291578/AppData/Local/Temp/anonymous5858848057389913831webdriver-profile/extensions/fxdriver@googlecode.com/components/command-processor.js:12221)
    

    如何找到元素?

1 个答案:

答案 0 :(得分:0)

它始终是表单中唯一的input元素吗?它是否始终具有相同的name属性?

在这种情况下,您可以尝试:

WebElement input = driver.findElement(By.Name("loginid"));

或者:

WebElement input = driver.findElement(By.CssSelector("#core_order_holder input"));