硒>如何使用Selenium Webdriver调用onClick javascript函数

时间:2015-03-16 09:11:56

标签: javascript security selenium

我遇到过一个问题,我正在尝试使用有效凭据提交表单。但由于在表单上实施了一些安全检查,我无法提交表格。进一步分析,我发现在提交表单时已经实现了onClick Javascript函数。任何人都可以建议,如何在这种情况下提交表单。

package erewards2;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class login_sib2 {

    public static void main(String[] args) {

        FirefoxDriver d1 = new FirefoxDriver();

                d1.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
                d1.get("https://sib2.erewardsredeem.com/fm/customer.html?action=login2");  
             WebElement e1 = d1.findElement(By.id("memberid"));
                 e1.sendKeys("harshcse30");
                 WebElement e2 = d1.findElement(By.id("memberpwd"));
                         e2.sendKeys("Ciquser2");
               e2.submit();

             // TODO Auto-generated method stub

                //close Firefox
                d1.close();

                // exit the program explicitly
                System.exit(0);

    }

}

上面代码的另一种变体,表格元素的属性值已更改,如下所示:

package erewards2;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class login_sib2 {

    public static void main(String[] args) {

        FirefoxDriver d1 = new FirefoxDriver();

                d1.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
                d1.get("https://sib2.erewardsredeem.com/fm/customer.html?action=login2");  
             WebElement e1 = d1.findElement(By.id("showmemberid"));
                 e1.sendKeys("harshcse30");
                 WebElement e2 = d1.findElement(By.id("showmemberpwd"));
                         e2.sendKeys("Ciquser2");
               e2.submit();

             // TODO Auto-generated method stub

                //close Firefox
                d1.close();

                // exit the program explicitly
                System.exit(0);

    }

}

我想要自动化的形式是:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <meta http-equiv="X-Frame-Options" content="deny" />
        <meta http-equiv="Cache-Control" content="max-age=0, no-cache, no-store, must-revalidate" />
        <meta http-equiv="Pragma" content="no-cache" />
        <meta http-equiv="Expires" content="-1" />
        <title> Login</title>
        <link rel="shortcut icon" type="image/x-icon" href="images/favicon.ico"/>
        <script type="text/javascript" src="/fm/js/login.js"></script>
        <script src="/fm/js/security/pbkdf2.js"></script>
        <script src="/fm/js/security/aes.js"></script>
        <link rel="stylesheet" href="/fm/styles/style.css" type="text/css" media="screen"/>
        <link rel="stylesheet" href="/fm/styles/forms.css" type="text/css" media="screen"/>
        <script type="text/javascript">
            window.history.forward();
            function noBack() {
                window.history.forward();
            }
        </script>
    </head>


    <script type="text/javascript">
        window.onload = function () {
            disableAutocompleteForAdmin();
        }
    </script>

    <body ondragstart="return false;" ondrop="return false;">
        <!--Start wrapper-->
        <div class="wrapper">
            <!--Start Header-->
            <div class="header">
                <div class="logoholder"><a id="home" href="user.html?action=login"></a></div>
                <div class="partner_logoblock"><img src="images/partner_logo.jpg"/></div>
            </div>
            <!--End Header-->
            <!--Start Main Container-->
            <div class="maincontainer">
                <!--Start Inner Container-->
                <div class="innercontainer">
                    <!--Start Login-->
                    <div class="logincontainer">
                        <form action="user.html?action=checkuser" method="POST" id="adminlogin" autocomplete="off" >  
                            <input type="hidden" name="_csrf" value="hzHmgJEwGgZiUP2cJO8P"/>

                                <input type="hidden" class="inputfiled" id="username" value="" name="email" maxlength="64" autocomplete="off"/>


                                <input  type="hidden" class="inputfiled" id="password" value="" name="password" maxlength="32" autocomplete="off" />


                        </form>
                        <h2>Admin / Merchant Login</h2>
                        <dl>

                            <dt>User Name</dt>
                            <dd>
                                <input type="text" class="inputfiled" id="showusername" value="" name="showusername" maxlength="64" autocomplete="off"/>
                            </dd>

                            <dt>Password</dt>
                            <dd>
                                <input  type="password" class="inputfiled" id="showpassword" value="" name="showusername" maxlength="32" autocomplete="off" />
                            </dd>


                            <dd>
                                <input name="submit" type="submit" value="Login" class="loginbtn" onclick="callvalidation();" />
                                <a href="user.html?action=resetp">Forgot Password?</a>
                            </dd>
                        </dl>
                        <input type="hidden" name="st" id="st" value="EE4F02F7EB9B81C3D07FDA36F5A1D7C38F073A7C3DFE3802584F3835037B820A" />

                        <div class="clearfix"></div>
                    </div>
                    <!--End Login-->
                </div>
                <!--End Inner Container-->
            </div>
            <!--End Main Container-->
        </div>
        <!--End wrapper-->
    </body>
</html>





 <link rel="shortcut icon" type="image/x-icon" href="images/favicon.ico" />
     <meta http-equiv="X-Frame-Options" content="deny" />

1 个答案:

答案 0 :(得分:0)

为什么不直接使用click()事件,而不是使用submit命令?