admin管理员组文章数量:1425899
I am trying to execute a javascript method on a webpage with the selenium WebDriver. I am able to execute my own script on the page, but not call the method from the page's HTML. Here is the HTML
function Search(){
if(check(document.forms[0],"searchResults")){
var fieldCode = "";
var fieldText = "";
if((document.forms[0].elements['prrSearchVO.selectedSearchCriteria.prrNumber'].value =="") && (!isPRRNoSelected()))
{
alert('PRR No. must be a selected field.');
unSelectAllOptions(document.forms[0].availableFieldsListArray);
unSelectAllOptions(document.forms[0].selectedFieldsList);
}
else
{
document.forms[0].excelRequested.value = "false";
document.forms[0].action = "prrSearchResults.do";
document.forms[0].submit();
}
}
}
If i attempt to submit the form manually it does not load properly. I can execute my own javascript using the code below. How can I execute the "Search" function from the page?
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("document.getElementById('fromIssueDate').removeAttribute('readOnly')");
I am trying to execute a javascript method on a webpage with the selenium WebDriver. I am able to execute my own script on the page, but not call the method from the page's HTML. Here is the HTML
function Search(){
if(check(document.forms[0],"searchResults")){
var fieldCode = "";
var fieldText = "";
if((document.forms[0].elements['prrSearchVO.selectedSearchCriteria.prrNumber'].value =="") && (!isPRRNoSelected()))
{
alert('PRR No. must be a selected field.');
unSelectAllOptions(document.forms[0].availableFieldsListArray);
unSelectAllOptions(document.forms[0].selectedFieldsList);
}
else
{
document.forms[0].excelRequested.value = "false";
document.forms[0].action = "prrSearchResults.do";
document.forms[0].submit();
}
}
}
If i attempt to submit the form manually it does not load properly. I can execute my own javascript using the code below. How can I execute the "Search" function from the page?
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("document.getElementById('fromIssueDate').removeAttribute('readOnly')");
Share
Improve this question
edited Jun 18, 2015 at 18:20
Vicky
3,0212 gold badges24 silver badges38 bronze badges
asked Jun 18, 2015 at 16:29
Kurter21Kurter21
731 gold badge2 silver badges8 bronze badges
1 Answer
Reset to default 2First of all you shouldn't execute javascript using WebDriver unless you really have no other option. Why doesn't the submit work? what did you try? In my experience sometimes click
doesn't trigger form submission, so instead you use submit
on the form element. Note that if you submit
on a non-form element its a no-op. So something like below should work,
WebElement email = driver.findElement(By.id("email"));
WebElement password = driver.findElement(By.id("password"));
WebElement submit = driver.findElement(By.id("submit"));
email.sendKeys("John");
email.password("foo");
submit.submit();
Going back to your original question, is Search()
a global function? Then you could do something like below, however like I said, WebDriver
doesn't remend you invoke javascript since that's not how real users would interact with your application
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("window.Search();");
本文标签: javaExecute javascript within webpage with selenium WebDriverStack Overflow
版权声明:本文标题:java - Execute javascript within webpage with selenium WebDriver - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745458772a2659226.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论