【问题标题】:xpath: Unable to locate Select dropdownxpath:无法找到选择下拉菜单
【发布时间】:2017-09-04 03:49:12
【问题描述】:

我收到以下异常,

无效的选择器:无法使用 xpath 定位元素 表达式 //select[@name='countryList 因为以下错误: SyntaxError:无法对“文档”执行“评估”:字符串 '//select[@name='countryList' 不是有效的 XPath 表达式。

这是我的html代码,

<form method="post" action="/eClaims/app" name="itemEditForm" id="itemEditForm" class="itemEditForm" accept-charset="utf-8">
<div style="display:none;"><input type="hidden" name="formids" value="TextField,wdd">
<input type="hidden" name="component" value="ClaimItemDetail.itemEditForm">
<input type="hidden" name="page" value="ClaimItemPage">
<input type="hidden" name="service" value="direct">
<input type="hidden" name="session" value="T">
<input type="hidden" name="If_87" value="T">
<input type="hidden" name="For_3" value="ecfqwcqw">

</div>
    <input type="text" name="TextField" value="" id="requestKeyItemId" style="visibility: hidden">
    <!-- START ORDERED Item detail table  -->   
        <div name="mainClaimItemDetailDiv">
            <table width="100%" bgcolor="#cccccc">

            </table>            
            <!-- show item information section START -->
            <table width="100%" bgcolor="#cccccc">
                <tbody><tr bgcolor="silver">
                    <th>
                        HP Item Ref.
                    </th>

                    <th>
                        Estimated Value
                        (
                        AUD
                        )<br>
                    </th>

                        <th>
                            Country of Origin
                        </th>                                       
                        <th>
                            Serial #
                        </th>                   
                    <th>
                        Estimated Value($)
                    </th>
                    <th>
                        <input type="text" name="clmid" value="" id="clmid" style="visibility: hidden;">
                    </th>
                </tr>
                <tr width="100%" bgcolor="#cccccc">
                    <td align="center">
                        56730270
                    </td>

                    <td>
                        252.79
                    </td>                   
                        <td align="center">

                                <!--<span jwcid="countryList_1" onchange="javascript:checkIfDropdownValueNull(this);" ></span> -->
                                <select name="countryList_1" id="countryList_1" style="width:50px;">
<option value="">--please select--</option>
<option value="AF">AF</option>

<option value="ZW">ZW</option>
</select>
                        </td>


                        <td align="center">

                                <input type="text" name="serialNo_2" value="5CD6340S04" id="serialNo_2" maxl="10" onfocus="javascript:onProductFocus(this);" size="10" onblur="javascript:onItemDataChange(this,10)">                           
                        </td>

                    <td align="center">
                        187.06
                    </td>
                    <td align="center">
                        <input type="submit" name="Submit" id="fetchitemBtn" class="fetchitemBtn" style="visibility: hidden;">
                    </td>
                </tr>
            </tbody></table>    
</form>

到目前为止,我使用了以下定位器,

//select[contains(@id,'countryList_1')]

//select[@id,'countryList_1']

//**[@id="itemEditForm"]/div[@name='mainClaimItemDetailDiv']/table[2]/tbody/tr[2]/td[11]

//**[@id="itemEditForm"]/div[@name='mainClaimItemDetailDiv']/following::select[@id='countryList_1']

虽然,最后一个 xpath 没有定位任何元素。 我用了很多,但都无济于事。

请帮忙。

提前致谢

【问题讨论】:

  • //select[@id='countryList_1'] 将找到一个 id = 'countryList_1' 的下拉列表。我已经在你的 HTML 上检查了它,它工作正常。检查拼写错误。
  • 我认为你的 xpath 有语法错误
  • 你的问题解决了吗?
  • 是的,使用了以下 xpath :
  • //*[@id="itemEditForm"]/div[@name='mainClaimItemDetailDiv']/table[2]/tbody/tr[2]/td[11]/select跨度>

标签: xpath selenium-webdriver


【解决方案1】:

在你的代码中使用下面的 xpath 它工作正常

您可以遵循以下任何 xpath:

//select[@id='countryList_1']

//select[@name='countryList_1']

//select[@id='countryList_1'][@name='countryList_1']

//select[contains(@name,'countryList_1')]

在您的错误日志中

"字符串 '//select[@name='countryList' 不是有效的 XPath 表达。”

这意味着你的 xpath 不正确,你在 xpath 中有一些语法错误

【讨论】:

    【解决方案2】:

    您的 xpath 缺少一个 ']'。使用正确的 xpath,如下所示

    //select[@name='countryList']
    

    【讨论】:

      猜你喜欢
      • 2021-06-28
      • 2023-03-31
      • 1970-01-01
      • 2015-01-03
      • 1970-01-01
      • 2018-09-23
      • 2019-07-30
      • 1970-01-01
      相关资源
      最近更新 更多