Home > database >  Python Selenium Chrome Unable to locate element no such element
Python Selenium Chrome Unable to locate element no such element

Time:07-10

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 >
                <span tipo="DATE">06/07/2022</span>
            </td>
        <td >
                <span tipo="DATE">Dados</span>
            </td>
        <td >
                
                    <a href="download_url"><img scr=...></a>
        </td>
</tr>
</tbody>
</table>

Thank you guys.

CodePudding user response:

  1. Use this extension on chrome: paThor to locate elements. If node is not found, check your Xpath.

  2. Check your window handles, If a new tab or popup is opened, you may not be checking the correct page.

  3. If there are multiple elements inside the div use relative Xpath like this:

Relative: //*[@id="linhaRow_14"]//td[4]/a or //*[@id="linhaRow_14"]//td[4]//a

Absolute: /*[@id="linhaRow_14"]/td[4]/a

CodePudding user response:

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

example of this iframe. If your element is inside the iframe, find_element you not work until you switch the focus of it.

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