java - Execute javascript within webpage with selenium WebDriver - Stack Overflow

I am trying to execute a javascript method on a webpage with the selenium WebDriver. I am able to execu

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
Add a ment  | 

1 Answer 1

Reset to default 2

First 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();");

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745458772a4628610.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信