Selenium找到嵌套在<table td =“”> </table> </textbox>中的<textbox>

时间:2014-03-04 09:55:08

标签: selenium xpath element

我想使用selenium根据表单属性从表单中选择特定的文本框。

我尝试了以下代码,但显示“无法找到ID ...”错误。

WebDriverWait waitt = new WebDriverWait(driver, 120);
waitt.until(ExpectedConditions.visibilityOfElementLocated(By.id("leadCaptureList_leadCapture_firstName")));

WebElement query = driver.findElement(By.xpath("//*  [@id='leadCaptureList_leadCapture_firstName']"));` 

我也尝试WebElement textbox = driver.findElement(By.id("leadCaptureList_leadCapture_firstName"));,但仍然没有运气。

selenium中有什么东西可以选择嵌套的文本框元素吗?

http://my.jetscreenshot.com/demo/20140304-c1dk-105kb.jpg(按CTRL +缩放图像) 这里(图中)是我页面的html结构。 在这张图片中,我突出显示了我想要定位的文本框。

下面评论中建议的HTML代码

<table id="page-container" border="0" cellpadding="0" cellspacing="0" width="100%">

 <tbody><tr>
    <td id="page-header">

<div id="top">

<div id="page-title" class="limiter clear-block">

<div id="header-top">

<div id="logo"><img id="logo-image" alt="Home" src="images/logo.png"></div>

<div id="header-right">

<div id="block-ad" class="block block-ad">

<div class="block-content clear-block ">



</div>

</div>



<div id="header-links">

<!-- added table for structure -->

<table style="display:block; float:right;" class="norubik" border="0" cellpadding="1" cellspacing="0" width="100%">

<tbody><tr>

<td align="center"><font color="#666"><a href="/user" style="color: red;font-size:10px;font-family:Verdana;color:#666;text-decoration: none">My Account </a> | <a style="color: red;font-size:10px;font-family:Verdana;color:#666;text-decoration: none" href="/logout">Logout</a></font></td>

</tr>

<tr>

<td align="center">

<span class="username"><!--Logged in as--> <strong>admin</strong>

<!-- Code for image -->

<div style="position:relative; top:2px; left:0px;">





    <img src="images/premier_small.png">







</div>

</span>

</td>

</tr>

</tbody></table>

</div> 









</div>

</div>

<div style="width: 100%; clear: both; text-align: left; margin-bottom: 6px; height:30px;">


<!-- begin menu div -->

<div class="AJXCSSMenuGdPbSLD"><!-- AJXFILE:ajxmenu.css -->

<div class="ajxmw">

<div class="ajxmw2">

<ul>



    <li style="position: static;" class=""><a class="ajxsub" href="/" title="Home"><b>Home</b>

    </a>

    <div style="display: none;" class="ajxdd">

    <div class="ajxsbg">



    </div>

    </div>

    </li>



    <li style="position: static;" class=""><a class="ajxsub" href="/node/3111" title="Getting Started"><b>Getting Started</b>

    </a>


    </li>



    <li style="position: static;" class=""><a class="ajxsub" href="/node/2962" title="Training"><b>Training</b>

    </a>

    <div style="display: none;" class="ajxdd">

    <div class="ajxsbg">



    <ul>



        <li style="position: static;" class=""><a class="ajxsub" href="/faq" title="FAQ">FAQ</a></li>



        <li style="position: static;" class=""><a class="ajxsub" href="/node/3431" title="Video Tutorials">Video Tutorials</a></li>



        <li style="position: static;" class=""><a class="ajxsub" href="/node/3138" title="Training Webinars">Training Webinars</a></li>



        <li style="position: static;" class=""><a class="ajxsub" href="/pub/training-schedule" title="Training Schedule">Training Schedule</a></li>



        <li style="position: static;" class=""><a class="ajxsub" href="/node/3735" title="Internet Marketing Webinars">Internet Marketing Webinars</a></li>



    </ul>



    </div>

    </div>

    </li>



    <li style="position: static;" class=""><a class="ajxsub" href="/reseller/ir/leadCaptureList" title="Leads"><b>Leads</b>

    </a> 
    </li>



    <li style="position: static;" class=""><a class="ajxsub" href="/node/2964" title="Affiliate"><b>Affiliate</b>

    </a> 

    </li>



    <li style="position: static;" class=""><a class="ajxsub" href="/catalog/179" title="Store"><b>Store</b>

    </a>

    <div style="display: none;" class="ajxdd">

    <div class="ajxsbg">



    <ul>



        <li style="position: static;" class=""><a class="ajxsub" href="/catalog/179" title="Memberships">Memberships</a></li>



        <li style="position: static;" class=""><a class="ajxsub" href="/catalog/219" title="Marketplace">Marketplace</a></li>



    </ul>



    </div>

    </div>

    </li>



    <li style="position: static;" class=""><a class="ajxsub" href="/" title="Resources"><b>Resources</b>

    </a>

    <div style="display: none;" class="ajxdd">

    <div class="ajxsbg">



    <ul>



        <li style="position: static;" class=""><a class="ajxsub" href="/node/3017" title="Support">Support</a></li>



        <li style="position: static;" class=""><a class="ajxsub" href="/reseller/calendar" title="Calendar/Events">Calendar/Events</a></li>



        <li style="position: static;" class=""><a class="ajxsub" href="/node/3440" title="Documents">Documents</a></li>



        <li style="position: static;" class=""><a class="ajxsub" href="/node/17" title="Documents">Documents</a></li>



        <li style="position: static;" class=""><a class="ajxsub" href="/reseller/ir/contactInfo" title="Upline Report">Upline Report</a></li>



        <li style="position: static;" class=""><a class="ajxsub" href="/node/3353" title="Feature Comparison">Feature Comparison</a></li>



        <li style="position: static;" class=""><a class="ajxsub" href="/node/3497" title="Developer (API)">Developer (API)</a></li>



    </ul>



    </div>

    </div>

    </li>







</ul>

</div>

</div>

</div>

<!-- end menu div --></div>

</div>



<div id="branding">

<div class="limiter clear-block">

<div class="breadcrumb clear-block"><span class="breadcrumb-link"><a href="/">LeadOutcome</a></span> <span class="breadcrumb-link"><a href="leadCaptureList.action">Leads</a></span> <span class="breadcrumb-link"><a href="leadCaptureList.action">Manage Leads</a></span>  </div>

</div>

</div>

</div>






    </td>
  </tr>


  <tr>
    <td id="content-container">

        <div id="page" class="clear-block page-content">

    <div id="content">
        <div class="content-wrapper clear-block">

<table border="0" cellpadding="0" cellspacing="0" width="100%">
    <tbody><tr>
        <td class="bhdr-container"><span class="bhdr"> Manage Leads </span></td>
    </tr>
</tbody></table>
<span style="color: blue; font-weight: bold"></span>
<span style="color: red; font-weight: bold"></span>


<form id="leadCaptureList" name="leadCaptureList" action="leadCaptureList.action" method="post">
<div><input name="advanceSearchClicked" value="No" id="advanceSearchClicked" type="hidden">

    </div><table class="wwFormTable">
    <input name="leadsToTransfer" value="" id="leadsToTransferId" type="hidden">
    <input name="deleteids" value="" id="leadCaptureList_deleteids" type="hidden">
    <input name="leadOrIR" value="Lead" id="leadCaptureList_leadOrIR" type="hidden">
    <input name="savedSearchCriteria.searchName" value="" id="leadCaptureList_savedSearchCriteria_searchName" type="hidden">
    <input name="savedSearchCriteria.published" value="" id="leadCaptureList_savedSearchCriteria_published" type="hidden">
    <input name="leadSearchName" value="" id="leadCaptureList_leadSearchName" type="hidden">
    <input name="cfValuesFromSessOrDB" value="false" id="leadCaptureList_cfValuesFromSessOrDB" type="hidden">

    </table><table class="hdr-section-search" border="0" cellpadding="0" cellspacing="0">
        <tbody><tr>
            <td>&nbsp;</td>
        </tr>
        <tr>
            <td>
            <table style="background-color: #F8F8F8" class="conbox4" border="0" cellpadding="2" cellspacing="0" width="100%">
                <tbody><tr>

                    <th colspan="4"><font size="2">Search Options (Search criteria can be combined)</font></th>
                    <th colspan="6" align="right"> 
                        <select id="currentlyOwnedLeads" name="currentlyOwnedLeads">

                            <option value="All">All leads </option>

                            <option value="No">Show all leads ever owned by me </option>
                            <option value="Yes">Show only leads currently owned by me </option>
                        </select> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                        <input name="" value="All" id="currentlyOwnedIdUsedForJavascript" type="hidden">
                     <input name="coForManageLeads" id="selectedSalesRepCo" value="" type="hidden">
                    <input name="id" value="" id="leadCaptureList_id" type="hidden"> Sales Rep: <span id="selectedIrCompany"></span> <a href="javascript:void()" id="searchIrButton"><img src="images/btn_find_IR.png" border="0"></a></th>

                    <th colspan="3" align="center">
                     <span onmousemove="ddrivetip(receiveSummaryMouseover,150)" onmouseout="hideddrivetip()">Receive Lead Summary:<select name="receiveLeadManagementSummaryEmails" id="summaryMailsField" onchange="setReceiveLeadManagementSummaryEmails();">
    <option value="false">No</option>
    <option value="true" selected="selected">Yes</option>


</select>
 </span> 
                    </th>
                </tr>


                <tr>
                    <td align="right">Lead Id:</td>
                    <td><input name="leadIdForSearch" maxlength="7" value="" id="leadCaptureList_leadIdForSearch" style="width:80px" type="text"></td>
                    <td align="right">First Name:</td>
                    <td><input name="leadCapture.firstName" value="test Lo" id="leadCaptureList_leadCapture_firstName" style="width:80px" type="text"></td>
                    <td align="right">Last Name:</td>
                    <td><input name="leadCapture.lastName" value="" id="leadCaptureList_leadCapture_lastName" style="width:80px" type="text"></td>
                    <td align="right">Email:</td>
                    <td><input name="leadCapture.email" value="" id="leadCaptureList_leadCapture_email" style="width:80px" type="text"></td>


                    <td align="right">Phone No.:</td>
                    <td><input name="leadCapture.phone" value="" id="phoneNo" style="width:80px" type="text"></td> 
                    <td align="right">Status:</td>
                    <td><select name="leadCapture.status" id="leadCaptureList_leadCapture_status">
    <option value="">-- Select Status --</option>
    <option value="New">New</option> 
    <option value="Converted">Converted</option>


</select>

                    </td>

                </tr>




                <tr id="advancedSearchRow1" style="display: none">
                        <td align="right">Company:</td>
                    <td><input name="leadCapture.company" value="" id="leadCaptureList_leadCapture_company" style="width:80px" type="text">     </td>

                    <td align="right">Address:</td>
                    <td><input name="leadCapture.address" value="" id="leadCaptureList_leadCapture_address" style="width:128px" type="text"></td>

                    <td align="right">City:</td>
                    <td><input name="leadCapture.city" value="" id="leadCaptureList_leadCapture_city" style="width:122px" type="text"></td>
                        <td align="right">State:</td>
                    <td><select name="leadCapture.state" id="leadCaptureList_leadCapture_state">
    <option value="">-- Select State --</option>
    <option value="AK">Alaska</option>



</select>

                    </td>
                        <td align="right">Zip Code:</td>
                    <td><input name="leadCapture.zip" value="" id="leadCaptureList_leadCapture_zip" style="width:80px" type="text"></td>

                </tr>
                <tr id="advancedSearchRow2" style="display: none">
                    <td align="right">Lead History:</td>
                    <td><input name="leadCapture.leadHistory" value="" id="leadCaptureList_leadCapture_leadHistory" style="width:80px" type="text"></td>

                    <td align="right">Category:</td>
                    <td><select name="leadCapture.leadCategory.id" id="leadCategorySearch1" style="width:122px">
    <option value="0" selected="selected">All Leads</option>
    <option value="2">Business Leads</option> 
    <option value="200">Webinar Follow up 12/4</option>


</select>
</td>

                    <td align="right">Custom Field Type:</td>
                    <td><select name="leadCapture.leadTypeObj.id" id="leadTypeId" style="width:122px" onclick="disableLeadType()" onchange="showCustomFields();">
    <option value="0" selected="selected">-Select Type-</option>
    <option value="16">Contacts</option>
    <option value="139">ZMS: How can we help you?</option>
    <option value="178">Gita</option>
    <option value="206">111 test</option>


</select>
</td>


                    <td align="right">Interest :</td>
                    <td><input name="leadCapture.interest" value="" id="leadCaptureList_leadCapture_interest" style="width:80px" type="text">
                    </td>
                    <td align="right">Score :</td>
                    <td colspan="2">
                    <select name="scoreLogicalOperator" id="leadCaptureList_scoreLogicalOperator">
    <option value="greater" selected="selected">Greater than</option>
    <option value="less">Less than</option>
    <option value="equal">Equal to</option>


</select>

                    &nbsp;
                    <input name="serachByleadScore" value="" id="leadCaptureList_serachByleadScore" style="width:80px" type="text">
                    </td>

                </tr>

                <tr id="advancedSearchRow3" style="display: none">
                    <td align="right">Lead Source:</td>
                    <td><input name="leadCapture.leadSource" value="" id="leadCaptureList_leadCapture_leadSource" style="width:80px" type="text"></td>

                    <td align="right">Ad Code:</td>
                    <td><input name="leadCapture.adCode" value="" id="leadCaptureList_leadCapture_adCode" style="width:80px" type="text"></td>

                    <td colspan="8" align="right"></td>

                </tr>


                <tr>
                    <td colspan="3" align="right" width="250px;">

                    <img src="images/btn_m_save_search.png" id="saveLeadSearchId" onclick="return popUpForSaveLeadSearch();" align="absmiddle" border="0">
                        <select name="savedSearchCriteria.id" id="leadCaptureList_savedSearchCriteria_id" onclick="disableLeadSearch()" onchange="return selectSearchCriteriaByName();">
    <option value="-1" selected="selected">--Select a search--</option>


</select>
</td>
                    <td colspan="6"><input style="display: none;" value="Delete Search" onclick="return deleteLeadSearchData();" id="deleteSearchButton" type="button"></td>

                    <td id="tdForAdvancedSearch" align="right"><a href="javascript:advancedSearch()" id="advancedSearchLink"><img src="images/btn_m_show_advanced.png" border="0"></a>
                    </td>

                    <td colspan="3" align="right"><input alt="true" src="images/btn_search.png" id="leadCaptureList_searchBtn" name="searchBtn" value="true" onclick="$('#pageNumber').val(1);return validSearchField();" type="image">

                    &nbsp;<input alt="Submit" src="images/btn_m_clear_search.png" id="leadCaptureList_0" value="Submit" onclick="clearAllSearchFields();" type="image">
</td>

                </tr>


            </tbody></table>
            </td>
        </tr>
    </tbody></table>
    <div id="customFieldsDiv"></div>
    <input name="noOfLeadsToBeDisplayed" value="250" id="noOfLeadsToBeDisplayedId" type="hidden">
    <input name="pagination" value="" id="pagination" type="hidden">
    <input name="pageNumber" value="1" id="pageNumber" type="hidden">
    <input name="backButton" value="false" id="backButton" type="hidden">
    <input name="nextButton" value="false" id="nextButton" type="hidden">
    <input name="saveSearchFlag" value="false" id="leadCaptureList_saveSearchFlag" type="hidden">

    </form></div>

</div></div></td></tr></tbody></table>

1 个答案:

答案 0 :(得分:0)

查找具有id属性的文本框几乎不会失败。如果它在那里,Selenium会发现它。如果在执行findElement命令时元素在DOM中不存在/不可见,那么Selenium将抛出异常。

您可以尝试使用WebDriver explict等待,看看它是否有效:

  WebElement textBox = (new WebDriverWait(driver, 10))
  .until(ExpectedConditions.visibilityOfElementLocated(By.id("leadCaptureList_leadCapture_firstName")));