Selenium Python关于陈旧元素随机失败

时间:2020-07-23 03:50:55

标签: python selenium selenium-webdriver xpath

我通过读取excel的输入并调用python方法来执行步骤,从而编写了用于自动化测试的机器人。在python中,我使用xpath查找文本的/ td。这是我的脚本,

def accounts_populate_filtros(self, str_name, bln_edit = False):
   path = ("(//* [contains(text(),'%s')]/ancestor::table[@id='ctl00_ContentPlaceHolder_tcUser_tpFilter_cblScopeFilters']/descendant::input/..)"% str(str_name))
   selected_element = WebDriverWait(self.driver, 20).until(EC.presence_of_element_located((By.XPATH, path)))
   WebDriverWait(self.driver, 20).until(EC.presence_of_element_located((By.XPATH, path)))
   self.accounts_list_checkbox_select(selected_element, bln_edit)

在该参数处,机器人将读取excel文件并传递要为变量“ str_name”查找的文本。使用此功能后,它将调用“ accounts_list_checkbox_select”以查找自己的复选框并打勾,此功能可以正常工作。

这是我要测试的Web代码。我要引用的文本是“ Branch TH1”。

By using our site, you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service.

<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>

实际上一次有很多输入,因此,根据excel的输入,此功能将被选择为每个机器人运行多次。但是当我跑步时,我发现它在此行随机失败,

selected_element = WebDriverWait(self.driver, 20).until(EC.presence_of_element_located((By.XPATH, path)))

错误显示在这里,

StaleElementReferenceException: Message: stale element reference: element is not attached to the page document

我不认为xpath是错误的,因为正如我所说,它是随机失败并通过的。每次运行时,它总是在此代码行失败,但输入不同。有时,某些输入运行通过,但是在不同的时间运行,该输入运行因上面的错误而失败。它有线。请帮忙。

0 个答案:

没有答案