RSelenium 中用于索引值列表的 XPath

XPath in RSelenium for indexing list of values

这里是html的例子:

<li class="index i1"
 <ol id="rem">
  <div class="bare">
   <h3>
      <a class="tlt mhead" href="https://www.myexample.com">

<li class="index i2"
 <ol id="rem">
  <div class="bare">
   <h3>
      <a class="tlt mhead" href="https://www.myexample2.com">

我想获取元素中每个 href 的值。是什么使列表成为第一个 li 中的 class,其中 class' 名称更改为 i1、i2。 所以我有一个计数器,去取值的时候改一下。

i <- 1
stablestr <- "index "
myVal <- paste(stablestr , i, sep="")

所以即使尝试使用这个

访问带有 myVal 索引的通用库
profile<-remDr$findElement(using = 'xpath', "//*/input[@li = myVal]")
profile$highlightElement()

或者 href 使用这个

profile<-remDr$findElement(using = 'xpath', "/li[@class=myVal]/ol[@id='rem']/div[@id='bare']/h3/a[@class='tlt']")

profile$highlightElement()

xpath有什么问题吗?

您的 HTML 结构无效。您的 <li> 标签没有正确关闭,您似乎混淆了 <ol><li>。但是为了这个问题,我假设结构和你写的一样,有正确关闭的 <li> 标签。

那么,构造myVal就不对了。当你想要 "index i1" 时,它会产生 "index 1""index i" 用于 stablestr.

现在是 XPath:

//*/input[@li = myVal]

这显然是错误的,因为你的 XML 中没有 input。此外,您没有在变量前加上 $。最后, * 似乎是不必要的。试试这个:

//li[@class = $myVal]

在你的第二个 XPath 中,也有一些错误:

/li[@class=myVal]/ol[@id='rem']/div[@id='bare']/h3/a[@class='tlt']
           ^                         ^                       ^
     missing $              should be @class    is actually 'tlt mhead'

前两个问题很容易解决。第三个不是。您可以使用 contains(@class, 'tlt'),但如果 class 是 tltt,这也将匹配,这可能不是您想要的。无论如何,它可能足以满足您的用例。固定 XPath:

/li[@class=$myVal]/ol[@id='rem']/div[@class='bare']/h3/a[contains(@class, 'tlt')]