【问题标题】:Selenium, Python. Gives no such element: Unable to locate element error whether the button is there硒,Python。没有给出这样的元素:无论按钮是否存在,都无法定位元素错误
【发布时间】:2017-02-21 11:48:59
【问题描述】:

我正在处理我的测试用例,其中包括向输入字段发送值以购买门票。但是对于这种情况,当我尝试定位名为 itemq_3728 的输入字段时,selenium 给了我无法定位元素错误,问题是页面每次重新打开页面时都会更改输入字段的名称。

如何找到输入字段? 我尝试了 XPath,但无法实现目标,也无法相对于票证名称编写它

<table id="bms_tickets" width="90%" cellspacing="5" cellpadding="0" class="bms_tickets table">
            <thead>
                <tr>
                    <th>NAME</th>
                    <th width="240px">PRICE</th>

                    <th width="100px">QUANTITY</th>

                </tr>
            </thead>
            <tbody id="resTypesTable">


<tr id="bms_restype_3728" class="bms_restype">
<td class="bms_restype_desc">
    Gen Ad


    <div style="font-size:10px;margin-left:5px;">

    </div>
</td>                       
<td class="bms_restype_price">


            $10.00
            <input type="hidden" name="pay_itemq_3728" value="10.00">   



</td>

<td class="bms_restype_qty">    

                <input type="text" name="itemq_3728" value="0" placeholder="1" min="1">
</td>

</tr>
            </tbody>
            </table>

【问题讨论】:

    标签: python google-chrome selenium xpath


    【解决方案1】:

    您可以使用cssSelector 找到它,如下所示:-

    driver.find_element_by_css_selector("td.bms_restype_qty > input[type='text']")
    

    或者,如果您有兴趣使用xpath 定位此元素,您可以使用Gen Ad 名称列文本来定位它,如下所示:-

    driver.find_element_by_xpath(".//td[normalize-space(.)='Gen Ad' and @class = 'bms_restype_desc']/following-sibling::td[@class='bms_restype_qty']/input")
    

    或者

    driver.find_element_by_xpath(".//tr[td[normalize-space(.)='Gen Ad']]/td[@class='bms_restype_qty']/input")
    

    【讨论】:

      【解决方案2】:

      希望这将有助于假设在页面加载后仅更改名称的数字路径: '//td[@class="bms_restype_qty"]//input[starts-­‐with(@name,"itemq")]'

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-11-22
        • 1970-01-01
        • 2021-05-31
        • 1970-01-01
        • 2022-11-14
        • 1970-01-01
        • 1970-01-01
        • 2017-12-08
        相关资源
        最近更新 更多