【问题标题】:Unable to find the xpath element for text in python无法在 python 中找到文本的 xpath 元素
【发布时间】:2020-07-20 11:07:40
【问题描述】:

我通过调用python方法编写机器人进行自动化测试。在 python 中,我使用 xpath 在 python 中查找文本“Branch TH1”。 这是参考,

str_name = 'Branch TH1'
text_element = self.driver.find_element(*AdminAccountsLocators.FILTROS_TABLE)
FILTROS_TABLE = (By.ID, 'ctl00_ContentPlaceHolder_tcUser_tpFilter_cblScopeFilters')

这是我的脚本,

path = ("//*[contains(text(),'%s')]/../input/.." %str_name)
WebDriverWait(self.driver, 20).until(EC.element_to_be_clickable((path)))
selected_element = text_element.find_element_by_xpath(path)

这是我要测试的网页代码,

<div id="ctl00_ContentPlaceHolder_tcUser_tpFilter" class="ajax__tab_panel" style="visibility: visible;">
            
                                                        <div id="ctl00_ContentPlaceHolder_tcUser_tpFilter_upEditViewFilters">
                
                                                                <div id="ctl00_ContentPlaceHolder_tcUser_tpFilter_pFilterTabContent" style="height:350px;width:570px;">
                    
                                                                    <div id="ctl00_ContentPlaceHolder_tcUser_tpFilter_pScopesControl">
                        
                                                                        <table style="width: 500px">
                                                                            <tbody><tr>
                                                                                <td align="right">
                                                                                    <span id="ctl00_ContentPlaceHolder_tcUser_tpFilter_lblApplication">Aplicación:</span>
                                                                                </td>
                                                                                <td align="left">
                                                                                    <select name="ctl00$ContentPlaceHolder$tcUser$tpFilter$ddlApplication" onchange="javascript:setTimeout('__doPostBack(\'ctl00$ContentPlaceHolder$tcUser$tpFilter$ddlApplication\',\'\')', 0)" id="ctl00_ContentPlaceHolder_tcUser_tpFilter_ddlApplication">
                            <option selected="selected" value="2">Aplicacion caja registradora.</option>
                            <option value="4">Aplicacion de Dashboard para Transactional Switch</option>
                            <option value="3">Aplicacion de partner portal.</option>
                            <option value="6">Aplicacion Prueba Symetric</option>
                            <option value="1">Aplicacion web central.</option>
                            <option value="5">Aplicación WebTeller</option>

                        </select>
                                                                                </td>
                                                                            </tr>
                                                                            <tr>
                                                                                <td align="right">
                                                                                    <span id="ctl00_ContentPlaceHolder_tcUser_tpFilter_lblScope">Alcance:</span>
                                                                                </td>
                                                                                <td align="left">
                                                                                    <div id="ctl00_ContentPlaceHolder_tcUser_tpFilter_upEditViewFiltersScopes">
                            
                                                                                            <select name="ctl00$ContentPlaceHolder$tcUser$tpFilter$ddlScope" onchange="javascript:setTimeout('__doPostBack(\'ctl00$ContentPlaceHolder$tcUser$tpFilter$ddlScope\',\'\')', 0)" id="ctl00_ContentPlaceHolder_tcUser_tpFilter_ddlScope">
                                <option value="6">Instancia de Aplicación</option>
                                <option selected="selected" value="4">Agencia</option>
                                <option value="5">Caja</option>
                                <option value="3">Cadena</option>
                                <option value="1">Operación</option>
                                <option value="2">Proveedor</option>
                                <option value="7">DES scope de prueba 2</option>

                            </select>
                                                                                        
                        </div>
                                                                                </td>
                                                                            </tr>
                                                                        </tbody></table>
                                                                    
                    </div>
                                                                    <div id="ctl00_ContentPlaceHolder_tcUser_tpFilter_pScopesChecks" style="text-align:left;">
                        
                                                                        <table border="0" style="border-collapse: collapse; height: 25px; width: 100%;">
                                                                            <tbody><tr>
                                                                                <td align="left">
                                                                                    <div id="ctl00_ContentPlaceHolder_tcUser_tpFilter_upEditViewFiltersFilterEnabledControls">
                            
                                                                                            <input id="ctl00_ContentPlaceHolder_tcUser_tpFilter_rbScopeFilterEnabled" type="radio" name="ctl00$ContentPlaceHolder$tcUser$tpFilter$rbScopeFilterStatus" value="rbScopeFilterEnabled" checked="checked"><label for="ctl00_ContentPlaceHolder_tcUser_tpFilter_rbScopeFilterEnabled">Habilitar</label>
                                                                                            <input id="ctl00_ContentPlaceHolder_tcUser_tpFilter_rbScopeFilterDisabled" type="radio" name="ctl00$ContentPlaceHolder$tcUser$tpFilter$rbScopeFilterStatus" value="rbScopeFilterDisabled" onclick="javascript:setTimeout('__doPostBack(\'ctl00$ContentPlaceHolder$tcUser$tpFilter$rbScopeFilterDisabled\',\'\')', 0)"><label for="ctl00_ContentPlaceHolder_tcUser_tpFilter_rbScopeFilterDisabled">Inhabilitar</label>
                                                                                        
                        </div>
                                                                                </td>
                                                                                <td align="right">
                                                                                    <input name="ctl00$ContentPlaceHolder$tcUser$tpFilter$txtScopeSearch" type="text" id="txtScopeSearch" maxlength="50" style="width: 200px;" onkeyup="window.ManageUser.EditView.eventHandlers.txtScopeSearch_OnKeyPress(this, event)" onkeydown="window.ManageUser.EditView.eventHandlers.txtScopeSearch_OnKeyPress(this, event)" value="Branch TH1">
                                                                                    <input type="submit" name="ctl00$ContentPlaceHolder$tcUser$tpFilter$btnScopeSearch" value="Buscar" id="btnScopeSearch" style="width:60px;">
                                                                                </td>
                                                                            </tr>
                                                                        </tbody></table>
                                                                        <div id="ctl00_ContentPlaceHolder_tcUser_tpFilter_upEditViewFiltersFilterList">
                            
                                                                                <table border="0" style="border-collapse: collapse; height: 310px; width: 100%;">
                                                                                    <tbody><tr>
                                                                                        <td colspan="2">
                                                                                            <div style="height: 285px; overflow-y: scroll;">
                                                                                                <table id="ctl00_ContentPlaceHolder_tcUser_tpFilter_cblScopeFilters" style="font-size:XX-Small;">
                                <tbody><tr>
                                    <td><input id="ctl00_ContentPlaceHolder_tcUser_tpFilter_cblScopeFilters_0" type="checkbox" name="ctl00$ContentPlaceHolder$tcUser$tpFilter$cblScopeFilters$0" onclick="javascript:setTimeout('__doPostBack(\'ctl00$ContentPlaceHolder$tcUser$tpFilter$cblScopeFilters$0\',\'\')', 0)" value="8121"><label for="ctl00_ContentPlaceHolder_tcUser_tpFilter_cblScopeFilters_0">Branch TH1</label></td><td></td>
                                </tr>
                            </tbody></table>
                                                                                            </div>
                                                                                        </td>
                                                                                    </tr>
                                                                                    <tr style="height: 24px;">
                                                                                        <td colspan="2">
                                                                                            
<table align="center" width="350px">
    <tbody><tr>
        <td align="left" style="width: 50px;">
            <input type="image" name="ctl00$ContentPlaceHolder$tcUser$tpFilter$cblScopeFiltersPager$lbtFirst" id="ctl00_ContentPlaceHolder_tcUser_tpFilter_cblScopeFiltersPager_lbtFirst" disabled="disabled" class="aspNetDisabled" src="../Images/first2.png">
        </td>
        <td align="left" style="width: 50px;">
            <input type="image" name="ctl00$ContentPlaceHolder$tcUser$tpFilter$cblScopeFiltersPager$lbtPrevious" id="ctl00_ContentPlaceHolder_tcUser_tpFilter_cblScopeFiltersPager_lbtPrevious" disabled="disabled" class="aspNetDisabled" src="../Images/previous2.png">
        </td>
        <td align="center" style="width: 150px;">
            <span id="ctl00_ContentPlaceHolder_tcUser_tpFilter_cblScopeFiltersPager_lblPageCounter">Page 1/1</span>
        </td>
        <td align="right" style="width: 50px;">
            <input type="image" name="ctl00$ContentPlaceHolder$tcUser$tpFilter$cblScopeFiltersPager$lbtNext" id="ctl00_ContentPlaceHolder_tcUser_tpFilter_cblScopeFiltersPager_lbtNext" disabled="disabled" class="aspNetDisabled" src="../Images/next2.png">
        </td>
        <td align="right" style="width: 50px;">
            <input type="image" name="ctl00$ContentPlaceHolder$tcUser$tpFilter$cblScopeFiltersPager$lbtLast" id="ctl00_ContentPlaceHolder_tcUser_tpFilter_cblScopeFiltersPager_lbtLast" disabled="disabled" class="aspNetDisabled" src="../Images/last2.png">
        </td>
    </tr>
</tbody></table>
                                                                                        </td>
                                                                                    </tr>
                                                                                </tbody></table>
                                                                            
                        </div>
                                                                    
                    </div>
                                                                
                </div>
                                                            
            </div>
                                                    
        </div>

This is when I find the xpath manually 你会看到我可以手动找到元素 "//*[contains(text(),'Branch TH1')]/../input/.." 作为红色框突出显示的 1of1,但它没有突出显示代码行(不确定这是否是重点)。

在我运行它之后,它抛出了一个错误: TypeError: find_element() 接受 1 到 3 个位置参数,但给出了 38 个

我不确定在运行机器人时是否有任何东西阻塞了我想要找到的 xpath。请帮忙。

【问题讨论】:

  • 但它没有在代码行突出显示(不确定是否是重点)这是因为 Chrome 84 版本的 Find 功能有问题并且没有突出显示匹配xpath 的元素。您可以在stackoverflow.com/a/62972580/5400362 找到有关它的更多信息
  • 不清楚您定位的是哪个元素。您是否定位名称为 Branch TH1 的复选框?
  • 我实际上有一个单独的函数来调用与其他页面一起工作的复选框,因为它必须被其他页面重用。对于这个函数,我只定位文本“Branch TH1”。
  • 对于 Chrome 84 的 bug - 它没有突出显示。但是如果它能够找到xpath的匹配元素,机器人应该捕获它并运行通过吗?
  • 这个问题仍然没有答案。目前尚不清楚您想要什么。您是要选中复选框还是只想要文本?除非我们不知道会发生什么,否则无济于事:(

标签: python selenium selenium-webdriver xpath robotframework


【解决方案1】:

你的 XPath 表达式:

//*[contains(text(),'%s')]/../input/..

会找到一个td 元素。如果要单击包含特定文本的复选框,则应定位 input 元素。感兴趣的 XPath 可能是:

//input[following-sibling::label[.="Branch TH1"]]

所以你可以使用类似的东西:

name = 'Branch TH1'
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[following-sibling::label[.='%s']]"% str(name)))).click()

进口:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

编辑:如果您需要查找特定文本来验证元素的存在,请使用:

name = 'Branch TH1'
print(WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH, "//*[contains(text(),'%s')]"% str(name)))))

【讨论】:

  • 谢谢@E.Wiest。实际上,我有一个单独的函数来调用与其他页面一起工作的复选框,因为它必须被其他页面重用。对于这个功能,我只想找到需要点击的文字而已。
  • 帖子已被编辑。您可以使用适当的功能检查元素的存在。当您说要单击文本时,实际上是在包含文本标签的 input 元素上。这就是为什么你应该使用我帖子的第一部分。
  • 谢谢! @E.Wiest present_of_element_located 可以解决我的问题。我不确定为什么我无法通过 EC.element_to_be_clickable 或 find_element_by_xpath 找到它。有何不同?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-06-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多