onchange event does not get fired on selenium type command

前端 未结 8 2082
予麋鹿
予麋鹿 2020-12-17 21:47

I am typing some value, on change do a total. But somehow, this event is not getting fired with selenium type command.

I also tried typeKey and typeAt ..But no succe

相关标签:
8条回答
  • 2020-12-17 22:25

    I faced the same issue but able to resolve this issue. Below code is for enter text in input field and fire onchange event.

    WebElement textBox = driver.findElement(By.xpath(xpath));
    textBox.sendKeys("Test");
    ((JavascriptExecutor) driver).executeScript("arguments[0].onchange", 
      Arrays.asList(textBox));
    

    Hope it will work!

    0 讨论(0)
  • 2020-12-17 22:26

    I had a similar problem, with a Dropdown list made with Ajax.
    As the user types in a field, the system displays AJAX divw with several options, each one as a link with target='#'

    And even worse, there was a function called on the onChange() that filled a system flag, and that flag would be used as a validation on the form.submit() (oh, the pain)

    Anyways, my solution for this:
    1 - Selenium sendKeys command so the Ajax div would appear

    <tr>
        <td>sendKeys</td>
        <td>id=txtTipoDocumento</td>
        <td>ipsum lorem</td>
    </tr>
    

    2 - wait for the link with the expected option to appear

    <tr>
        <td>waitForElementPresent</td>
        <td>link=ipsum lorem</td>
        <td></td>
    </tr>
    

    3 - selenium clickAt the link

    <tr>
        <td>clickAt</td>
        <td>link=ipsum lorem</td>
        <td>10,20</td>
    </tr>
    


    4 - Here is the ONE of the catches: manually fire the onChange() AND blur events. Also, foce the browser to set focus on different field

     <tr>
            <td>fireEvent</td>
            <td>id=txtTipoDocumento</td>
            <td>blur</td>
        </tr>
        <tr>
            <td>fireEvent</td>
            <td>id=selSerie</td>
            <td>change()</td>
        </tr>
        <tr>
            <td>fireEvent</td>
            <td>id=selSerie</td>
            <td>blur</td>
        </tr>
        <tr>
            <td>focus</td>
            <td>id=imgDataElaboracao</td>
            <td></td>
        </tr>
    

    5 - Finally, to be sure, I made Selenium do execute the ClickAt() command on the Submit button of the forme, between a mouseDown and MouseUp commands

    <tr>
        <td>mouseDown</td>
        <td>id=btnSalvar</td>
        <td></td>
    </tr>
    <tr>
        <td>focus</td>
        <td>id=btnSalvar</td>
        <td></td>
    </tr>
    <tr>
        <td>clickAt</td>
        <td>id=btnSalvar</td>
        <td>10,20</td>
    </tr>
    


    Not elegant, but it worked.

    0 讨论(0)
提交回复
热议问题