Home > database >  How to correctly write a nested xpath for selenium
How to correctly write a nested xpath for selenium

Time:03-17

I'm trying to use Rselenium seleniumPipes to access this zipfile by name: "PUB004_PRE_20220316.zip"

<tr id="l1_VkFMX1BSRV9SUl8yMDIyMDMxMi56aXA"  title="PUB004_PRE_20220316.zip
Hoy 02:20 PM (4.22 MB)">
  <td >
   <div >
     <span >.</span>
     <span ></span>
     <span ></span>
     <span >PUB004_PRE_20220316.zip</span>
   </div>
 </td>
 <td >lectura</td>
 <td >Hoy 02:20 PM</td>
 <td >4.22 MB</td>
 <td >Archivo ZIP</td>
</tr>

Picture of the whole code

But cant seem to get the xpath correctly.

Some of my tries:

select_file <- robot$findElement(
  "xpath", "//tr[.//td[.//div[@class='elfinder-cwd-file-wrapper']]]//span[@class='elfinder-cwd-filename']//*[text()='PUB004-PRE-20220316.zip']")
select_file$clickElement()

select_file <- robot$findElement(
  "xpath", "//*[@class='elfinder-cwd-file-wrapper']//*[@class='elfinder-cwd-filename']//*[text()='PUB004-PRE-20220316.zip']")
select_file$clickElement()

select_file <- robot$findElement(
  "xpath", "//*[@class='elfinder-cwd-filename']//*[text()='PUB004-PRE-20220316.zip']")
select_file$clickElement()

This is the webpage. I want to download a the zip files.

Note: I need to do it by name because I'm interested in downloading the file programmatically by date (20220316).

CodePudding user response:

If I understand you correctly, you want to click on tr parent element containing the span element containing the desired file name in it's text. Right?
But the tr element itself contains that string in it's title.
So, why not simply to use this XPath: ?

"//tr[contains(@title,'20220316')]"

CodePudding user response:

Seems you were close enough, instead of _ character it should have been - character and should have been PUB004-PRE-20220316.zip


Solution

To identify the element you can use either of the following locator strategies:

  • Using xpath and the innerText:

    select_file <- robot$findElement("xpath", "//span[text()='PUB004-PRE-20220316.zip']")
    
  • Using xpath with class and the innerText:

    select_file <- robot$findElement("xpath", "//span[@class='elfinder-cwd-filename' and text()='PUB004-PRE-20220316.zip']")
    
  • Related