【问题标题】:Selenium locate <textbox> nested in <table td>Selenium 定位 <textbox> 嵌套在 <table td>
【发布时间】:2014-04-05 18:47:41
【问题描述】:

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

我尝试了下面的代码,但它显示“无法找到 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>

【问题讨论】:

  • 任何帮助,这可能吗?如果我的上述问题不清楚,请在这里告诉我
  • 从不将代码发布为图片。没有人能够复制这样的东西。如果代码太大而无法将其放入问题中,请将相关部分放入问题中,并将其他所有内容放入 Gist 或您希望的任何位置。
  • 具有 id 属性的文本框无论是嵌套元素还是非嵌套元素都不会产生影响。页面渲染时文本框是否可见?还是动态的?
  • @JensErat 可以按照您的建议执行...
  • @AbhijeetVaikar 我认为为此目的我什至尝试检查WebDriverWait waitt = new WebDriverWait(driver, 120); waitt.until(ExpectedConditions.visibilityOfElementLocated(By.id("leadCaptureList_leadCapture_firstName"))); 但仍然是同样的错误。 & 文本框不是动态的。相同的代码在我的本地系统中运行在常规的 Firefox 上,但是当我将它上传到 linux 服务器上时,我遇到了这个问题。

标签: selenium xpath element


【解决方案1】:

查找具有 id 属性的文本框实际上应该不会失败。如果 Selenium 存在,它将找到它。如果执行 findElement 命令时元素在 DOM 中不存在/不可见,则 Selenium 将抛出异常。

您可以像这样尝试 WebDriver 显式等待,看看它是否有效:

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

【讨论】:

  • 在生成的 html 代码中,我看到这个文本框 id 已经加载并且可见。但为什么它不选择。有什么建议吗?
猜你喜欢
  • 2013-11-08
  • 1970-01-01
  • 2021-01-11
  • 1970-01-01
  • 1970-01-01
  • 2019-12-17
  • 1970-01-01
  • 2017-05-26
  • 1970-01-01
相关资源
最近更新 更多