0

I write the robot for automation testing by calling the python method. In python, I use xpath to find the text 'Branch TH1' in python. This is reference,

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

Here's my script,

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)

This is the web code that I want to test,

<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 You will see that I can manually find the element "//*[contains(text(),'Branch TH1')]/../input/.." as 1of1 found in red box highlight, but it does not highlight at the line of code (not sure if this is the point).

After I run the it, it thrown me an error: TypeError: find_element() takes from 1 to 3 positional arguments but 38 were give

I'm not sure if anything blocks the xpath that I want to find when running the robot. Please help.

Pom JPz
  • 3
  • 1
  • _but it does not highlight at the line of code (not sure if this is the point)_ it is because Chrome 84 version's Find feature is buggy and does not highlight the matching elements of xpath. you can find more info about it at https://stackoverflow.com/a/62972580/5400362 – Dev Jul 20 '20 at 11:31
  • It is not clear which element you locating. Are you targeting checkbox of name _Branch TH1_? – Dev Jul 20 '20 at 14:56
  • I actually have a separate function to call for clicking the checkbox which working well with other pages as it has to be reused for others. For this function, I target the text 'Branch TH1' only. – Pom JPz Jul 21 '20 at 03:34
  • For the buggy of Chrome 84 - it's not highlighted. But if it can find the matching element of xpath, the robot should capture it and run pass? – Pom JPz Jul 21 '20 at 04:18
  • The question is still unanswered. It is not clear what you want. Do you want to check box to be checked or you just want the text? Unless we dont know what is expected can't help :( – Dev Jul 21 '20 at 06:18
  • I try to find the /td of the text 'Branch TH1' actually, thanks. – Pom JPz Jul 21 '20 at 09:33

1 Answers1

1

Your XPath expression :

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

will find a td element. If you want to click on the checkbox containing your specific text, you should target the input element. XPath of interest could be :

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

So you can use something like :

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

Imports :

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

EDIT : If you need to find a specific text to verify the presence of an element, use :

name = 'Branch TH1'
print(WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH, "//*[contains(text(),'%s')]"% str(name)))))
E.Wiest
  • 5,425
  • 2
  • 7
  • 12
  • Thanks @E.Wiest. I actually have a separate function to call for clicking the checkbox which working well with other pages as it has to be reused for others. For this function, I just want to find the text that need to be clicked only. – Pom JPz Jul 21 '20 at 03:32
  • Post has been edited. You can check the presence of the element with the appropriate function. When you say you want to click on the text, it's in fact on the `input` element which contains the text label. That's why you should use the first part of my post. – E.Wiest Jul 21 '20 at 09:37
  • Thanks! @E.Wiest presence_of_element_located can solve my problem. I'm not sure why I can't find it by EC.element_to_be_clickable or find_element_by_xpath. How different? – Pom JPz Jul 21 '20 at 10:28