尽管Xpath可用,但会抛出找不到元素错误

时间:2018-11-30 14:51:22

标签: selenium selenium-webdriver selenium-chromedriver

    <!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="__EVENTARGUMENT" id="__EVENTARGUMENT" value="">
<input type="hidden" name="__VIEWSTATE" id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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>
                        <input name="txtCity" type="text" id="txtCity" tabindex="2" class="StyledText">&nbsp;
                    </td>
                </tr>
                <tr>
                    <td>
                        <span id="lblAccountState" class="StyledLabel" style="width:110px;">Account State </span>
                    </td>
                    <td>
                        <select name="ddlState" id="ddlState" tabindex="3" class="StyledDropDown" style="width:180px;">
	<option value="--Select--">--Select--</option>
	<option value="AK">Alaska</option>
	<option value="AL">Alabama</option>
	<option value="AR">Arkansas</option>
	<option value="AZ">Arizona</option>
	<option value="CA">California</option>
	<option value="CO">Colorado</option>
	<option value="CT">Connecticut</option>
	<option value="DC">District of Columbia</option>
	<option value="DE">Delaware</option>
	<option value="FL">Florida</option>
	<option value="GA">Georgia</option>
	<option value="HI">Hawaii</option>
	<option value="IA">Iowa</option>
	<option value="ID">Idaho</option>
	<option value="IL">Illinois</option>
	<option value="IN">Indiana</option>
	<option value="KS">Kansas</option>
	<option value="KY">Kentucky</option>
	<option value="LA">Louisiana</option>
	<option value="MA">Massachusetts</option>
	<option value="MD">Maryland</option>
	<option value="ME">Maine</option>
	<option value="MI">Michigan</option>
	<option value="MN">Minnesota</option>
	<option value="MO">Missouri</option>
	<option value="MS">Mississippi</option>
	<option value="MT">Montana</option>
	<option value="NC">North Carolina</option>
	<option value="ND">North Dakota</option>
	<option value="NE">Nebraska</option>
	<option value="NH">New Hampshire</option>
	<option value="NJ">New Jersey</option>
	<option value="NM">New Mexico</option>
	<option value="NV">Nevada</option>
	<option value="NY">New York</option>
	<option value="OH">Ohio</option>
	<option value="OK">Oklahoma</option>
	<option value="OR">Oregon</option>
	<option value="PA">Pennsylvania</option>
	<option value="RI">Rhode Island</option>
	<option value="SC">South Carolina</option>
	<option value="SD">South Dakota</option>
	<option value="TN">Tennessee</option>
	<option value="TX">Texas</option>
	<option value="UT">Utah</option>
	<option value="VA">Virginia</option>
	<option value="VT">Vermont</option>
	<option value="WA">Washington</option>
	<option value="WI">Wisconsin</option>
	<option value="WV">West Virginia</option>
	<option value="WY">Wyoming</option>

</select>
                    </td>
                </tr>
                <tr>
                    <td>
                    </td>
                    <td>
                    </td>
                </tr>
                <tr>
                    <td colspan="3">
                        <table border="0" width="80%">
                            <tbody><tr>
                                <td>
                                    <input type="submit" name="btnSearch" value="Search" onclick="return Validate_Page();" language="javascript" id="btnSearch" tabindex="4" class="UsabilityButton" style="width:80px;">
                                </td>
                                <td style="display: none">
                                    <input type="submit" name="btnReset" value="" id="btnReset" class="UsabilityButton" style="height:24px;width:80px;">
                                </td>
                                <td style="display: none">
                                    <input type="submit" name="btnCreateNew" value="" onclick="enableDisableButtons(false,this);" language="javascript" id="btnCreateNew" class="UsabilityButton" disabled="disabled" style="height:24px;width:170px;">
                                </td>
                            </tr>
                        </tbody></table>
                    </td>
                </tr>
            </tbody></table>
            <div class="clearline">
                &nbsp;
            </div>
            <div class="clearline">
                &nbsp;
            </div>
            <div class="clearline">
                &nbsp;
            </div>
            <div class="clearline">
                &nbsp;
            </div>
            <table width="950px">
                <tbody><tr>
                    <td>
                        <table>
                            <tbody><tr>
                                <td>
                                    
                                    <div>

</div>
                                </td>
                            </tr>
                        </tbody></table>
            </td></tr></tbody></table>
            
            <div id="msg" style="display: none;Font-size:20px;" class="StyledLabel_link">
            
                Can't find what you are looking for? Enter the information manually&nbsp;<a id="A1" href="javascript:__doPostBack(&#39;A1&#39;,&#39;&#39;)">here</a>&nbsp; or
                <a id="A2" href="javascript:__doPostBack(&#39;A2&#39;,&#39;&#39;)">search again</a>
            </div>
        </div>
    </div>
    <div>
        
        <table>
            <tbody><tr>
                <td style="padding-left: 350px">
                </td>
                <td>
                    <img src="./Processingimg.gif" id="img1" style="float: right; visibility: hidden;" alt="Loading">
                </td>
                <td>
                    <input name="btnprog" type="button" id="btnprog" value="Processing.." style="visibility: hidden" align="left" alt="Loading">
                </td>
            </tr>
        </tbody></table>
        
    </div>
    </form>


</body></html>

我尝试过的简单代码:

    driver.findElement(By.xpath("//input[@name='txtAccountName']")).sendKeys("CRSsolutions_test_" + today + "_tanksafe");

网页: http://ussbyintv8057.acetst.com/Enterprise_CRS/Login/login1.aspx

进入此页面的步骤:

  1. 使用用户名adyami和密码1234登录 2.点击顶部的新政策链接
  2. 从pf产品列表中,选择“坦克安全”
  3. 单击下一步
  4. 这是我们需要提供帐户名称和状态的页面

HTML代码

2 个答案:

答案 0 :(得分:3)

很可能您只需要等待元素出现即可。使用explicit wait代替findElement

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

此模式最多允许等待10秒,但如果满足条件,则返回速度更快。条件elementToBeClickable确保该字段是可单击的,这是输入值的前提。

答案 1 :(得分:0)

查看U是否未通过使用wait进行定位,那么您需要使用绝对xpath来定位该元素。或者您也可以使用CSS选择器。

有时,对于有角度的网页,正常的xpath无法定位元素。如果它是有角度的,那么我建议使用量角器工具来自动进行硒

否则,如果相对xpath找不到您的元素,则绝对xpath将起作用。