简体   繁体   中英

Selenium Webdriver, click a button that isn't really a button

So, i've got this button, thats not a button but is actually three separate table cells(don't know why). But I cannot click it, even by clicking the table, or a point specified that is within the table's confines. I'm wondering if there is a better way to go about clicking this "button" or at least running the script behind it so that the form can be submitted all the same.

    <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>

Anyways, this is part of a ui and it only gets enabled when I fill the form out to standard. The Java code that I am using to click the "button" is below:

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

My expected result is that the WebDriver clicks the "button" and then moves onto the next part of the test.

My end result is that the driver does not click the "button" and so stays on the ui, unable to continue.

You will have to use JavascriptExecutor in order to perform clicking on an elements which are not links or buttons (Selenium itself is just not able to do that).

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

我之前从未见过“ fire”属性,您会使用“ onClick”吗?

If you want to directly execute the javascript specified on any of the attributes, you can use getAttribute() to get the script and then execute using the JavascriptExecutor .

For eg: to execute the script contained in the fire attribute of the div element:

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM