I'm working on a web-scraping project and I'm new to RSelenium and need help in selecting a value from a drop down list. I've looked around through posts and found that using an Xpath is pretty handy when making a selection from a drop down list. I was able to write some code that appears to be activating a click from the drop down menu, seen below, but I don't know how to actually force a selection from a specific menu option (e.g., Current Title).
webElem <- remDr$findElement(using = 'xpath', value = "//*[@data-v-106f7212='']")
webElem$clickElement()
I want to point out that when I physically make a selection from the drop down menu, the class="searchCategorySelect"
from the first div bracket changes to class="searchCategorySelect catSelected"
. The title value from this bracket also changes along with the value within the <span class="searchCategorySelectLabel"...>
bracket. I've illustrated these changes with the examples down below.
I was using this answer from this post as inspiration for my code. The webs-scraping code from this example selects a season by using an Xpath and selecting a data-option-name
containing the season of choice. The HTML from my page isn't structured in this manner and I'm stuck on how to effectively make a selection using RSelenium given the structure of my HTML.
Any advice or help would be useful and appreciated.
Default:
<div class="searchCategorySelect" data-v-106f7212="" data-v-c62fe6f4="" title="All Resources">
<select class="srselect srselectTopbar" data-v-106f7212="" aria-label="Select Search Category">
<option class="" data-v-106f7212="" value="CurrentSrTitle">Current Title</option>
<option class="" data-v-106f7212="" value="All">All Resources</option>
<option class="" data-v-106f7212="" value="Images">Images</option>
<option class="" data-v-106f7212="" value="Dictionary">Dictionary</option>
<option class="" data-v-106f7212="" value="Media">Media</option>
<option class="" data-v-106f7212="" value="Alerts">Alerts</option>
<option class="" data-v-106f7212="" value="News">News</option>
<option class="" data-v-106f7212="" value="EBMcalc">EBMcalc</option>
<option class="" data-v-106f7212="" value="Help">Help</option>
</select>
<div class="searchCategorySelectVisual" data-v-106f7212="" role="presentation">
<span class="searchCategorySelectLabel" data-v-106f7212="">All Resources</span>
<span class="triangleGlyph" data-v-106f7212="">
<svg class="vsvg" data-v-887bca16="" data-v-106f7212="" focusable="false" aria-label="" role="presentation" aria-hidden="true">
<use data-v-887bca16="" xlink:href="#white_triangle2" role="presentation"></use>
<!---->
<!---->
</svg>
</span>
</div>
</div>
When a selection is made:
<div class="searchCategorySelect catSelected" data-v-106f7212="" data-v-c62fe6f4="" title="Current Title">
<select class="srselect srselectTopbar" data-v-106f7212="" aria-label="Select Search Category">
<option class="" data-v-106f7212="" value="CurrentSrTitle">Current Title</option>
<option class="" data-v-106f7212="" value="All">All Resources</option>
<option class="" data-v-106f7212="" value="Images">Images</option>
<option class="" data-v-106f7212="" value="Dictionary">Dictionary</option>
<option class="" data-v-106f7212="" value="Media">Media</option>
<option class="" data-v-106f7212="" value="Alerts">Alerts</option>
<option class="" data-v-106f7212="" value="News">News</option>
<option class="" data-v-106f7212="" value="EBMcalc">EBMcalc</option>
<option class="" data-v-106f7212="" value="Help">Help</option>
</select>
<div class="searchCategorySelectVisual" data-v-106f7212="" role="presentation">
<span class="searchCategorySelectLabel" data-v-106f7212="">Current Title</span>
<span class="triangleGlyph" data-v-106f7212="">
<svg class="vsvg" data-v-887bca16="" data-v-106f7212="" focusable="false" aria-label="" role="presentation" aria-hidden="true">
<use data-v-887bca16="" xlink:href="#white_triangle2" role="presentation"></use>
<!---->
<!---->
</svg>
</span>
</div>
</div>
I can not test the answer because I don't have the url, but usually, when I have to choose an element from a list, I do this:
You could create a xpath as in this way:
"//*/option[@value='Images']"
...
"//*/option[@value='Help']"
This could be work
remDr$findElement(using = 'xpath', value = "//*/option[@value='Images']")$clickElement()
but shouldn't it work, probably you have to click (activate) the box before to choose an element (as in this answer).
If you provide the url I could complete my answer and check it.