2017-10-20 115 views
0

對於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 &amp;&amp; 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 &amp;&amp; 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> 
+0

顯示HTML,你要選擇 –

+0

@VitaliyMoskalyuk編輯我的職位 – snoepdogg

+0

更新的答案是什麼=) –

回答

0

所有這些都將工作:

//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']