Selenium>如何使用Selenium Webdriver调用onClick javascript函数
Selenium> How to call onClick javascript function using Selenium Webdriver
我遇到了一个问题,我正在尝试提交具有有效凭据的表单。但由于表格上进行了一些安全检查,我无法提交表格。经过进一步分析,我发现在提交表单时已经实现了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() 事件?
相关文章:
- Javascript:selenium Web驱动程序isDisplayed()不工作
- 如何使jQuery插件函数可调用以供独立使用,而不在集合上操作
- D3在一个调用中绘制不同的SVG形状,没有可见性
- 如何从Java/scala调用js美化程序
- 如何调用这个匿名 JavaScript 函数
- 如何从模块链中调用函数.导出到节点中
- 我需要从php调用javascript或jquery
- Chrome开发工具(如何知道我在调用哪个javascript对象)
- Selenium如何批处理许多isElementDisplayed调用
- Selenium>如何使用Selenium Webdriver调用onClick javascript函数
- 如何验证是否使用 Selenium 在 javascript 对象上调用了某些方法
- 如何从Selenium中调用JavaScript函数
- 为什么这个JS函数会'当它通过selenium.getEval()调用时不起作用
- 当使用selenium-webdriverjs运行mocha测试时,调用堆栈没有太大帮助
- 为什么 Ajax 调用在 WebDriverWait (Selenium) 期间被冻结
- 如何通过selenium调用JS文件
- Selenium和异步JavaScript调用
- 调用selenium-ide中的js文件函数
- 在selenium RC中过滤javascript XHR调用
- selenium + chrome. filessystem . chooseentry =无效的调用页面错误