如何正确编写 selenium 的嵌套 xpath

How to correctly write a nested xpath for selenium

我正在尝试使用 Rselenium+seleniumPipes 按名称访问此 zip 文件:“PUB004_PRE_20220316.zip”

<tr id="l1_VkFMX1BSRV9SUl8yMDIyMDMxMi56aXA" class="elfinder-cwd-file elfinder-ro ui-selectee ui-draggable-handle" title="PUB004_PRE_20220316.zip
Hoy 02:20 PM (4.22 MB)">
  <td class="elfinder-col-name">
   <div class="elfinder-cwd-file-wrapper">
     <span class="elfinder-cwd-icon elfinder-cwd-icon-application elfinder-cwd-icon-zip">.</span>
     <span class="elfinder-perms"></span>
     <span class="elfinder-lock"></span>
     <span class="elfinder-cwd-filename">PUB004_PRE_20220316.zip</span>
   </div>
 </td>
 <td class="elfinder-col-perm">lectura</td>
 <td class="elfinder-col-date">Hoy 02:20 PM</td>
 <td class="elfinder-col-size">4.22 MB</td>
 <td class="elfinder-col-kind">Archivo ZIP</td>
</tr>

Picture of the whole code

但似乎无法正确获取 xpath。

我的一些尝试:

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是网页。我想下载一个 zip 文件。

注意:我需要按名称执行此操作,因为我有兴趣按日期 (20220316) 以编程方式下载文件。

如果我没理解错的话,您想单击 tr parent 元素,span 元素在其文本中包含所需的文件名。对吗?
但是 tr 元素本身在其标题中包含该字符串。
那么,为什么不简单地使用这个 XPath:?

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

看来你已经足够接近了,而不是 _ 字符应该是 - 字符,应该是 PUB004-PRE-20220316.zip


解决方案

要识别元素,您可以使用以下任一方法 :

  • 使用 xpathinnerText:

    select_file <- robot$findElement("xpath", "//span[text()='PUB004-PRE-20220316.zip']")
    
  • xpathclassinnerText 结合使用:

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