Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
156 views
in Technique[技术] by (71.8m points)

selenium - Though Xpath is available, element not found error is thrown

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<!-- saved from url=(0078)http://ussbyintv8057.acetst.com/Enterprise_CRS/CAT06/newquote/FindAccount.aspx -->
<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE8">
    <title>Account Search</title>
    <link href="./ACE(2).css" type="text/css" rel="stylesheet">
    <script language="javascript" src="./CommonFunctions.js(2).download"></script>
    
    <style type="text/css">
        #btnprog
        {
            border-style: none;
            border-color: inherit;
            border-width: medium;
            width: 73px;
            opacity: 1;
            background: none;
        }
    </style>
    
    <script type="text/javascript">

        function CheckAccountName(txtbox, minlength) {
            // alert(txtbox);
            txt = document.getElementById(txtbox);
            if (txt.value.length < minlength) {
                alert("Enter Account Name with minimum " + minlength.toString() + " characters.");
                window.event.returnValue = false;
            }
            else {
                //enableDisableButtons(false,document.getElementById("btnSearch"));
                enableDisableButtons(true, document.getElementById("btnSearch"));
            }
        }


        function Validate_Page() {

            var Name = document.getElementById("txtAccountName").value;
            var Result = Name.search(/</);
            if (Result != -1) {
                document.getElementById("txtAccountName").value = "";
                document.getElementById("txtAccountName").focus();
                alert('Account Name cannot contain < sign');
                return false;

            }

            else if (document.getElementById("txtAccountName").value == "") {
                alert("Enter Account Name ");
                return false;
            }

            else if (document.getElementById("ddlState").selectedIndex == 0) {
                alert("Enter State/province");
                return false;
            }
            else {
                //enableDisableButtons(false,document.getElementById("btnSearch"));
                document.getElementById("img1").style.visibility = 'visible';
                document.getElementById("btnprog").style.visibility = 'visible';
                enableDisableButtons(true, document.getElementById("btnSearch"));
                return true;
            }

        }





        function doClick(buttonName, e) {
            //the purpose of this function is to allow the enter key to 
            //point to the correct button to click.
            var key;

            if (window.event)
                key = window.event.keyCode;     //IE
            else
                key = e.which;     //firefox

            if (key == 13) {
                //Get the button the user wants to have clicked
                var btn = document.getElementById(buttonName);
                if (btn != null) { //If we find the button click it
                    btn.click();
                    event.keyCode = 0
                }
            }
        }

        function enableDisableButtons(enablestatus, caller) {
            str = true;
            if (document.getElementById("btnSearch") != null) {
                document.getElementById("btnSearch").disabled = str;
            }
            if (document.getElementById("btnReset") != null) {
                document.getElementById("btnReset").disabled = str;
            }
            if (document.getElementById("btnCreateNew") != null) {
                document.getElementById("btnCreateNew").disabled = str;
            }
            if (enablestatus) {
                if (typeof (Page_ClientValidate) == 'function') {
                    Page_ClientValidate();
                    if (Page_IsValid) {
                        __doPostBack(caller.name, '');
                        //EnableDisableAll(true);
                    }
                    else {
                        str = false;
                        if (document.getElementById("btnSearch") != null) {
                            document.getElementById("btnSearch").disabled = str;
                        }
                        if (document.getElementById("btnReset") != null) {
                            document.getElementById("btnReset").disabled = str;
                        }
                        if (document.getElementById("btnCreateNew") != null) {
                            //document.getElementById("btnCreateNew").disabled = str;
                            if (document.getElementById("ddlState").selectedIndex == 0) {
                                document.getElementById("btnCreateNew").disabled = true;
                            }
                            else {
                                document.getElementById("btnCreateNew").disabled = str;
                            }
                        }
                    }
                }
                else {
                    __doPostBack(caller.name, '');
                    //EnableDisableAll(true);
                }
            }
            else {
                __doPostBack(caller.name, '');
                // EnableDisableAll(true);
            }

        }

    </script>
    <meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
    <meta content="Visual Basic .NET 7.1" name="CODE_LANGUAGE">
    <meta content="JavaScript" name="vs_defaultClientScript">
    <meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
</head>
<body>
    <form name="form1" method="post" action="http://ussbyintv8057.acetst.com/Enterprise_CRS/CAT06/newquote/FindAccount.aspx" id="form1">
<input type="hidden" name="__EVENTTARGET" id="__EVENTTARGET" value="">
<input type="hidden" name="__EVENTARGUMEN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TWjdNZT0qvVwsOMGiLuQvIqbcTqPop/eDh73bFDb3o/3+RNhKYZRyLI/JCw3SM7EYOtB50s9KtzV32aY/fsHFSww831bJBWEysr91J1s80si6J4tlcCrjMgiLIPInwC6mxszE9ZOp8TcrL3fIfKve19aYGEDSt5XjCjVJCVsFsCKboAP+JqI7Q3IBUsmHQ2BmQFJKsZWxcWdADgm+57lVa9ceg9X/+WBFuskGv2m5i0fDOISrjTgP0tIm6JU2ojfTXqYuC/NlEaCq+ULYAXDP1bgsp4d/7KFscpFTrKxjc2dVOkiiFdBW7XVOCDTJah/N1isKA3NPHmeTOQ==">

<script type="text/javascript">
<!--
var theForm = document.forms['form1'];
if (!theForm) {
    theForm = document.form1;
}
function __doPostBack(eventTarget, eventArgument) {
    if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
        theForm.__EVENTTARGET.value = eventTarget;
        theForm.__EVENTARGUMENT.value = eventArgument;
        theForm.submit();
    }
}
// -->
</script>


<input type="hidden" name="__VIEWSTATEGENERATOR" id="__VIEWSTATEGENERATOR" value="FB9A1659">
<input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="LAqN9KqJRJt8nwVwM/CS3iT+g6ogrSCD/nT4j26niWlWT7OlPx2pl4mOPcliyZpAu7nwQIOxr07USDx5OA5zWPYe03ruoenkHm2Z2EtBFVB93BB8rk2sI7NQcxwqzadcrDyjvTZV/Ol7zKp6JvdWl5P/CGO9gvvuLW/YnYnSlMj9BHly07tqXngW3NRHv3o2x1odM7N1HwLteLNrhFxP2f45dX2yGjF3NULUFl7yuRBWhGqxqkQnd41RUchVbjCTZOy8SbQ19TKW+MOhuwf7OyLCeCaqmeWa/dYtuonJuAqazLjVDPWq0CWUBqts9NkUoOriL7dThNoHjV982e7p09SSo7b7sfI4Tgl7ePGmnzJAn+v7zYU62km/9KRnRVmOnQJEF/Dz476r8HNHrYgTD4Y3XQql2jl04Vr+kwt0d+KgRnLNWrBlEYXTALCm41kFzr2Vkuw+AzWNYdlRULtTK09OR3oaHEj1KfN3yRh3FIUPR5bH8ov5BozHKfDqfoc/RaoCvNTlptmjRuMI2L073euGlHigQCuBMCgfR+MOfkrA4F6FA+XcvKhl+939QryKFCdlAnSeeG4zLZmjvAIOmHly/8qKfT7TGBK1bKK45qU1p1XbuhQ8u/Hn9AL7+N+bDDneDJiZL6kVX/Wojz2DfMrKVePPVweatZCpCiIg99ZMKS1QeqRv6LdryPEypZfHfbWICLW1+WLdV6+c3jX2OKzFEk8Wwj0ifWkH42m6ROjG+4eePY4+80oHfQeyNDorEDnGH0JXq25sKxQ1DeF/A2JSsO5TXKU+fT+StVFnsj0dmX5gBOocWgKR+9bv8HzBbVVSjzXkTl5Njc2pphVweCOeBiH+ZP0IAw3mfOSXBCkgqFbUkpA18gTy8h6s88LOOSHNg3/VyD8d2ehbVa0cJTKpaUe6quESAo+ZP5bWfxZKKH3w8d/jxFzRkPgMN7elBAh02bggg0+sPXTmnp1g2BIOglrN1xgdFIiVs+Zdi7fN4wL9pZ5TcN7SLO0/vUGrbig/0rQATrN4UmHo+kdLhUM4l/2BKqg88ogR1KDxKMWDCePqVtN3ZOZWorJw3ORKZle1osLLSSBdnEd/gYSR+/NtJCC9i6V/7zt9AeOSav8KxcmH7nKeKfvltdbCEEYPVJpC3x1bEg3E5ikhtEBNcq22i4Et4XW6KBH2XoaNrqrASz5lX3nNjwKd5hHoiYjBE6yAqmRcOpIEPznzWM1RxqfoO4d36fmta0hJBwcCEo6DXzn+bRTbC9Qc/anGYfcr0YB9MEoexGwOjwaARyRoS7BgUhmfUYOaYERKJwdbfImQA6jFaWI6h2xXIcoCPMsdEK9eVbvGzPiDCJuumvn6I6WyVvuv2ke2w/bIsdDMlzrIi7yyUkTm7ZEvAKs=">
    
    <div class="page" align="left">
        <br>
        <div align="left" class="PageLabel" style="padding-top: 10px;">
            <span>FIND ACCOUNT</span>
        </div>
        <div class="greenbreak" align="left">
        </div>
        <div class="pagecontent">
            <div class="clearline">
                &nbsp;
            </div>
            <div class="StyledLabel leftdiv">
                Search for Applicant Information
            </div>
            <div class="clearline">
                &nbsp;
            </div>
            <div class="clearline">
                &nbsp;
            </div>
            <table style="top: 520; left: 120; width: 500px; margin-top: 5px; margin-left: 5px" border="0">
                <tbody><tr>
                    <td class="StyledLabel" align="left" colspan="2">
                        <p>
                            <span id="lblMessage" class="StyledLabel" style="color:Red;background-color:Transparent;font-family:Arial;font-size:8pt;font-weight:bold;width:110px;"></span></p>
                    </td>
                </tr>
                <tr>
                    <td>
                        <span id="lblAccountName" class="StyledLabel" style="width:110px;">Account Name</span>
                    </td>
                    <td>
                        <input name="txtAccountName" type="text" id="txtAccountName" tabindex="1" class="StyledText" onkeypress="doClick(&#39;btnSearch&#39;,event)">
                    </td>
                </tr>
                <tr>
                    <td>
                        <span id="lblCity" class="StyledLabel" style="width:110px;">City</span>
                    </td>
                    <td>
   

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Most likely you just need to wait for element to appear. Use explicit wait instead of findElement:

WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement txtAccountName = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@name='txtAccountName']")));
txtAccountName.sendKeys("CRSsolutions_test_" + today + "_tanksafe");

This pattern allows to wait for up to 10 seconds, but will return faster if condition is satisfied. Condition elementToBeClickable makes sure the field is clickable, which is a prerequisite for value to be inputed.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...