使用xpath查找嵌套div(RSelenium)
问题描述:
对于RSelenium和xpaths而言,虽然有些问题已经被问及有关此主题,但似乎无法使其适用于我。我正在尝试下载名为“随时间变化的兴趣”页面上的第一个小部件的CSV文件。此选项没有唯一的ID /类等,但是类“widget-actions-item”中的第三个类,其中类“widget-action-item-text”包含“CSV”。使用xpath查找嵌套div(RSelenium)
library(RSelenium)
library(wdman)
driver <- rsDriver(browser = "firefox")
remDr <- driver[["client"]]
remDr$open
remDr$navigate("http://trends.google.com/trends/explore?cat=3")
webElem1 <- remDr$findElement("class name", "widget-actions-menu")
webElem1$clickElement()
上面的代码打开网页和第一个小工具菜单有三个下载选项,其中之一是CSV。
webElem2 <- remDr$findElement("xpath", paste0("//div[@class='widget-actions-content']//[@class='widget-actions-item'][3]"))
webElem2$clickElement()
驱动程序找不到此元素。
<div class="widget-actions-content" ng-show="widgetActions.menuOpen" aria-hidden="false" style="">
<!----><button class="widget-actions-item" ng-if="showActionButton()" ng-click="embed()" title="Embed" track="['Widget', type, 'Embed']" ve-tracking="" jslog="39389; track:generic_click">
<span class="widget-actions-item-icon embed-image flip-rtl"></span>
<div class="widget-actions-item-text">
Embed
</div>
</button><!---->
<!----><button class="widget-actions-item" ng-if="isEditor || share" ng-click="share()" title="Share" track="['Widget', type, 'Share']" ve-tracking="" jslog="39390; track:generic_click">
<span class="widget-actions-item-icon share-image flip-rtl"></span>
<div class="widget-actions-item-text">
Share
</div>
</button><!---->
<!----><button class="widget-actions-item" ng-if="isEditor || (export && showActionButton())" ng-click="export()" title="CSV" track="['Widget', type, 'Export']" ve-tracking="" jslog="39388; track:generic_click">
<span class="widget-actions-item-icon csv-image flip-rtl"></span>
<div class="widget-actions-item-text">
CSV
</div>
</button><!---->
<help-dialog ng-show="!$root.globals.isDesktopMode && helpDialog" class="widget-actions-item ng-hide" show-annotation="true" data="helpDialog" aria-hidden="true"><button class="help-icon-button" ng-click="openDialog()" title="Help">
<i class="material-icons-extended help-icon-color">help_outline</i>
<div class="widget-actions-item-text" ng-show="showAnnotation" aria-hidden="false">
Help
</div>
</button>
</help-dialog>
</div>
答
所有这些都将工作:
//button[contains(.,'CSV')]
//button[div[@class='widget-actions-item-text'][contains(text(),'CSV')]]
//button[div[@class='widget-actions-item-text' and contains(text(),'CSV')]]
//button[@title='CSV']
显示HTML,你要选择 –
@VitaliyMoskalyuk编辑我的职位 – snoepdogg
更新的答案是什么=) –