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

Selenium> How to call onClick javascript function using Selenium Webdriver

本文关键字:Selenium 调用 onClick javascript 函数 Webdriver 何使用      更新时间:2023-09-26

我遇到了一个问题,我正在尝试提交具有有效凭据的表单。但由于表格上进行了一些安全检查,我无法提交表格。经过进一步分析,我发现在提交表单时已经实现了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" />
与其使用

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