R - Rselenium - navigate drop down menu / list / box using = 'id'

…衆ロ難τιáo~ 提交于 2019-12-22 12:02:06

问题


How can I navigate a dynamic drop down list via " using = 'id' " (e.g., remDr$findElement(using='id', value="main_ddYear") ?

I can find and click using findElement. After clicking I could send "down arrow" keys (keystrokes) and an "enter" if I know how many arrows down my targeted selection is.

Sample of the page source

<select name="main$ddYear" onchange="javascript:setTimeout(&#39;__doPostBack(\&#39;main$ddYear\&#39;,\&#39;\&#39;)&#39;, 0)" id="main_ddYear" class="groupTextBox">
<option selected="selected" value="2017">2017</option>
<option value="2016">2016</option>
<option value="2015">2015</option>
<option value="2014">2014</option>
<option value="2013">2013</option>
</select>

I would like to do something similar to the solution in this post but with 'id' instead of 'xpath' . I could not adapt the xpath solution.

Another solution in Java used a "Select" class which I did not find referenced in the quick start tutorial or documentation.

I will post a separate question about how to scrape the drop down list of options / values.


回答1:


With a little knowledge about XPath, adapting the linked solution which using XPath for your case should be straightforward, for example :

option <- remDr$findElement(using = 'xpath', "//select[@id='main_ddYear']/option[@value='2014']")
option$clickElement()

Brief explanation about the XPath :

  • //select[@id='main_ddYear'] : Find <select> element, anywhere in the HTML, where id attribute value equals 'main_ddYear'
  • /option[@value = '2014'] : From such <select> element, return child <option> where value attribute value equals '2014'.


来源:https://stackoverflow.com/questions/39713466/r-rselenium-navigate-drop-down-menu-list-box-using-id

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!