Selenium Webdriver,单击一个非按钮的按钮

时间:2013-10-30 21:55:59

标签: java javascript jquery selenium selenium-webdriver

所以,我有这个按钮,这不是一个按钮,但实际上是三个独立的表格单元格(不知道为什么)。但我无法点击它,即使单击表格,或指定在表格范围内的点。我想知道是否有更好的方法来点击这个“按钮”或至少运行它后面的脚本,以便表格可以提交所有相同。

    <div title="Submit" id="saveUserButton" style="float: right;" fire="function() {
  return __method.apply(null, [this].concat($A(arguments)));
}" observe="function() {
  return __method.apply(null, [this].concat($A(arguments)));
}" stopObserving="function() {
  return __method.apply(null, [this].concat($A(arguments)));
}" visible="function() {
  return __method.apply(null, [this].concat($A(arguments)));
}" toggle="function() {
  return __method.apply(null, [this].concat($A(arguments)));
}" hide="function() {
  return __method.apply(null, [this].concat($A(arguments)));
}" show="function() {
  return __method.apply(null, [this].concat($A(arguments)));
}" remove="function() {
  return __method.apply(null, [this].concat($A(arguments)));
}" update="function() {
  return __method.apply(null, [this].concat($A(arguments)));
}" replace="function() {
  return __method.apply(null, [this].concat($A(arguments)));
}" insert="function() {
  return __method.apply(null, [this].concat($A(arguments)));
}" wrap="function() {
  return __method.apply(null, [this].concat($A(arguments)));
}" inspect="function() {
  return __method.apply(null, [this].concat($A(arguments)));
}" recursivelyCollect="function() {
  return __method.apply(null, [this].concat($A(arguments)));
}" ancestors="function() {
  return __method.apply(null, [this].concat($A(arguments)));
}" descendants="function() {
  return __method.apply(null, [this].concat($A(arguments)));
}" firstDescendant="function() {
  return __method.apply(null, [this].concat($A(arguments)));
}" immediateDescendants="function() {
  return __method.apply(null, [this].concat($A(arguments)));
}" previousSiblings="function() {
  return __method.apply(null, [this].concat($A(arguments)));
}" nextSiblings="function() {
  return __method.apply(null, [this].concat($A(arguments)));
}" siblings="function() {
  return __method.apply(null, [this].concat($A(arguments)));
}" match="function() {
  return __method.apply(null, [this].concat($A(arguments)));
}" up="function() {
  return __method.apply(null, [this].concat($A(arguments)));
}" down="function() {
  return __method.apply(null, [this].concat($A(arguments)));
}" previous="function() {
  return __method.apply(null, [this].concat($A(arguments)));
}" next="function() {
  return __method.apply(null, [this].concat($A(arguments)));
}" select="function() {
  return __method.apply(null, [this].concat($A(arguments)));
}" adjacent="function() {
  return __method.apply(null, [this].concat($A(arguments)));
}" identify="function() {
  return __method.apply(null, [this].concat($A(arguments)));
}" readAttribute="function() {
  return __method.apply(null, [this].concat($A(arguments)));
}" writeAttribute="function() {
  return __method.apply(null, [this].concat($A(arguments)));
}" getHeight="function() {
  return __method.apply(null, [this].concat($A(arguments)));
}" getWidth="function() {
  return __method.apply(null, [this].concat($A(arguments)));
}" classNames="function() {
  return __method.apply(null, [this].concat($A(arguments)));
}" hasClassName="function() {
  return __method.apply(null, [this].concat($A(arguments)));
}" addClassName="function() {
  return __method.apply(null, [this].concat($A(arguments)));
}" removeClassName="function() {
  return __method.apply(null, [this].concat($A(arguments)));
}" toggleClassName="function() {
  return __method.apply(null, [this].concat($A(arguments)));
}" cleanWhitespace="function() {
  return __method.apply(null, [this].concat($A(arguments)));
}" empty="function() {
  return __method.apply(null, [this].concat($A(arguments)));
}" descendantOf="function() {
  return __method.apply(null, [this].concat($A(arguments)));
}" scrollTo="function() {
  return __method.apply(null, [this].concat($A(arguments)));
}" getStyle="function() {
  return __method.apply(null, [this].concat($A(arguments)));
}" getOpacity="function() {
  return __method.apply(null, [this].concat($A(arguments)));
}" setStyle="function() {
  return __method.apply(null, [this].concat($A(arguments)));
}" setOpacity="function() {
  return __method.apply(null, [this].concat($A(arguments)));
}" getDimensions="function() {
  return __method.apply(null, [this].concat($A(arguments)));
}" makePositioned="function() {
  return __method.apply(null, [this].concat($A(arguments)));
}" undoPositioned="function() {
  return __method.apply(null, [this].concat($A(arguments)));
}" makeClipping="function() {
  return __method.apply(null, [this].concat($A(arguments)));
}" undoClipping="function() {
  return __method.apply(null, [this].concat($A(arguments)));
}" cumulativeOffset="function() {
  return __method.apply(null, [this].concat($A(arguments)));
}" positionedOffset="function() {
  return __method.apply(null, [this].concat($A(arguments)));
}" absolutize="function() {
  return __method.apply(null, [this].concat($A(arguments)));
}" relativize="function() {
  return __method.apply(null, [this].concat($A(arguments)));
}" cumulativeScrollOffset="function() {
  return __method.apply(null, [this].concat($A(arguments)));
}" getOffsetParent="function() {
  return __method.apply(null, [this].concat($A(arguments)));
}" viewportOffset="function() {
  return __method.apply(null, [this].concat($A(arguments)));
}" clonePosition="function() {
  return __method.apply(null, [this].concat($A(arguments)));
}" getElementsBySelector="function() {
  return __method.apply(null, [this].concat($A(arguments)));
}" childElements="function() {
  return __method.apply(null, [this].concat($A(arguments)));
}" _extendedByPrototype="function() { }" getElementsByClassName="function() {
  return __method.apply(null, [this].concat($A(arguments)));
}" childOf="function() {
  return __method.apply(null, [this].concat($A(arguments)));
}" morph="function() {
  return __method.apply(null, [this].concat($A(arguments)));
}" visualEffect="function() {
  return __method.apply(null, [this].concat($A(arguments)));
}" highlight="function() {
  return __method.apply(null, [this].concat($A(arguments)));
}" fade="function() {
  return __method.apply(null, [this].concat($A(arguments)));
}" appear="function() {
  return __method.apply(null, [this].concat($A(arguments)));
}" grow="function() {
  return __method.apply(null, [this].concat($A(arguments)));
}" shrink="function() {
  return __method.apply(null, [this].concat($A(arguments)));
}" fold="function() {
  return __method.apply(null, [this].concat($A(arguments)));
}" blindUp="function() {
  return __method.apply(null, [this].concat($A(arguments)));
}" blindDown="function() {
  return __method.apply(null, [this].concat($A(arguments)));
}" slideUp="function() {
  return __method.apply(null, [this].concat($A(arguments)));
}" slideDown="function() {
  return __method.apply(null, [this].concat($A(arguments)));
}" pulsate="function() {
  return __method.apply(null, [this].concat($A(arguments)));
}" shake="function() {
  return __method.apply(null, [this].concat($A(arguments)));
}" puff="function() {
  return __method.apply(null, [this].concat($A(arguments)));
}" squish="function() {
  return __method.apply(null, [this].concat($A(arguments)));
}" switchOff="function() {
  return __method.apply(null, [this].concat($A(arguments)));
}" dropOut="function() {
  return __method.apply(null, [this].concat($A(arguments)));
}" getInlineOpacity="function() {
  return __method.apply(null, [this].concat($A(arguments)));
}" forceRerendering="function() {
  return __method.apply(null, [this].concat($A(arguments)));
}" setContentZoom="function() {
  return __method.apply(null, [this].concat($A(arguments)));
}" collectTextNodes="function() {
  return __method.apply(null, [this].concat($A(arguments)));
}" collectTextNodesIgnoreClass="function() {
  return __method.apply(null, [this].concat($A(arguments)));
}" getStyles="function() {
  return __method.apply(null, [this].concat($A(arguments)));
}">
        <table id="saveUserButton_GlassButtonInnerTable" style="height: 24px;" border="0" cellSpacing="0" cellPadding="0">
            <tbody>
                <tr>
                    <td style="background-image: url(//GlassButton/EnabledLeft.gif); MIN-WIDTH: 6px; width: 6px;"/>
                    <td style="background-image: url(//GlassButton/EnabledCenter.gif); PADDING-BOTTOM: 0px; padding-left: 5px; padding-right: 5px; background-repeat: repeat-x; height: 24px; color: white; overflow: auto; padding-top: 0px;">
                    <td style="background-image: url(//GlassButton/EnabledRight.gif); MIN-WIDTH: 6px; width: 6px;"/>
                </tr>
            </tbody>
         </table>
     </div>

无论如何,这是ui的一部分,只有在我将表格填写到标准时才会启用。我用来点击“按钮”的Java代码如下:

  //Assuming WebDriver driver works correctly
  WebDriver driver;
  WebElement submit = driver.findElement(By.id("saveUserButton"));
  submit.click();

我的预期结果是WebDriver单击“按钮”,然后转到测试的下一部分。

我的最终结果是驱动程序没有点击“按钮”,因此停留在ui上,无法继续。

3 个答案:

答案 0 :(得分:0)

您必须使用JavascriptExecutor才能执行点击非链接或按钮的元素(Selenium本身无法做到这一点)。

JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("document.querySelector(\"a[id$='saveUserButton']\").click()");

答案 1 :(得分:0)

我以前从未见过“火”属性,你不会使用“onClick”吗?

答案 2 :(得分:0)

如果要直接执行在任何属性上指定的javascript,可以使用getAttribute()获取脚本,然后使用JavascriptExecutor执行。

例如:执行fire元素的div属性中包含的脚本:

WebElement submit = driver.findElement(By.id("saveUserButton"));

//get the script on 'fire'
String script = submit.getAttribute("fire");

//execute
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript(script);