Search code examples
pythonseleniumelementlocate

Python Selenium Chrome Unable to locate element no such element


Hello I just got hired in my first job as junior eng. (I printed a img). I must program a robot that acess a webpage, login, and download a file which is located on a dynamic table, There are always 15 records. Everyday they add one in the bottom of the table and remove the last one, organized by date.

I tried to use Time.Sleep() to wait the page to load completely and even used WebDriverWait and expected_conditions together to wait until it locates the element. enter image description here This is my code:

username_input = wdriver.find_element(By.XPATH, '//*[@id="j_username"]').send_keys(user)
password_input = wdriver.find_element(By.XPATH, '//*[@id="j_password"]').send_keys(password)
login_buttom = wdriver.find_element(By.XPATH, '//*[@id="submit"]').click()

--url where the dynamic table is located
wdriver.get('https://jfbricks.sienge.com.br/sienge/8/index.html#/common/page/2590')

--I have to acess the 'a' element in html where the download link is located
element = wdriver.find_element(By.XPATH,'//*[@id="linhaRow_14"]/td[4]/a')

I get this message:
Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="linhaRow_14"]/td[4]/a"}

this is the section I must access: I have to catch the last updated date, and access <a > 
 to  download the file.

I tried to locate the table 'father' using 'tabelaRow' and also tried to locate the tr with id = 'linhaRow_14'
<table id="tabelaRow">
<tbody>
<tr id="linhaRow_14">
        <td class="spwCelulaGrid spwLinhaGridPar">
                <span tipo="DATE">06/07/2022</span>
            </td>
        <td class="spwCelulaGrid spwLinhaGridPar aR">
                <span tipo="DATE">Dados</span>
            </td>
        <td class="spwCelulaGrid spwLinhaGridPar alignCenter">
                
                    <a href="download_url"><img scr=...></a>
        </td>
</tr>
</tbody>
</table>

Thank you guys.


Solution

  • Okay guys it took a while to find the error. Basically, in my case, this table was loaded inside an iframe element, the xpath from the element that I wanted was being searched in the outerMost parent element, but the Xpath is in fact inside the element of the second html.

    Solution, use: wdriver.switch_to.frame('id_of_the_iframe_you_want_to_inspect')

    This function switches the focus to a specific frame. you can pass other parameters to identify the iframe you must target to SELENIUM to inspect and find your element.

    If your element is inside the iframe, find_element won't work until you switch the focus of it. Example of this iframe

     <head>
     <body>
       <div>
         <main> 
           <iframe id="iFramePage"  scr="path.."
             ***this iframe carries another html 
               <html>
                 <head> ...
                     ...
                    <table>
                       <tr id="linhaRow_14">
                   ... 
               ...