RSelenium Select Dropdown/ComboBox Value from Java

2020-08-01 05:44发布

问题:

I am trying to download a csv file from a website using RSelenium. I get to the page, which is a Crystal Report Viewer, and select the element of the export button and click that button. Then an export window appears with a file format selection. I am able to find the element of the dropdown list but I am having issues with the values of the list. The values appear to be coming from a very long javascript script like the following:

<script type="text/javascript" language="JavaScript">
`{"args":`

{"args":{"id":"CrystalReportViewer_exportUI","availableFormats":[{"name":"Crystal Reports (RPT)","value":"CrystalReports"},{"name":"PDF","value":"PDF"},{"name":"Microsoft Excel (97-2003)","value":"MSExcel"},{"name":"Microsoft Excel (97-2003) Data-Only","value":"RecordToMSExcel"},{"name":"Microsoft Word (97-2003)","value":"MSWord"},{"name":"Microsoft Word (97-2003) - Editable","value":"EditableRTF"},{"name":"Rich Text Format (RTF)","value":"RTF"},{"name":"Separated Values (CSV)","value":"CharacterSeparatedValues"},{"name":"XML","value":"XML"}]` `</script>

The dropdown box looks like the following:

I am able to find the element related to the above dropdown box.

When I click the dropdown menu the list above shows. When I am looking at the HTML element while selecting one of the values, the value I select gets updated in the HTML code--I'm assuming from the Javascript list. It looks like the following after I click "Seperated Values (CSV)":

<div id="id_name" class="icontext" style="white-space:nowrap;text-overflow:ellipsis;overflow:hidden;width:249px">Separated Values (CSV)</div>

I tried to use the following code but it does not work:

WebElemFileFormat <- remDr$findElement(using = 'id', "id_name") WebElemFileFormat$clickElement() WebElemFileFormat$sendKeysToElement(list("Separated Values (CSV)"))

I read this post Selecting a javascript dropdown but not sure how that translates to RSelenium. Many thanks in advance.

回答1:

After doing some more research I decided to go a different route with this. I'll leave this up in case someone comes across a similar issue. I noticed that after playing around with the dropdown box I could use the down arrow or tab to moved down the list.

I used the following code to complete what I wanted to in the question above:

`WebElemFileFormat <- remDr$findElement(using = 'id', "id_name")
remDr$setImplicitWaitTimeout(milliseconds = 10000)
WebElemFileFormat$clickElement()
remDr$sendKeysToActiveElement(list(key = 'tab'))
remDr$sendKeysToActiveElement(list(key = 'tab'))
remDr$sendKeysToActiveElement(list(key = 'tab'))
remDr$sendKeysToActiveElement(list(key = 'tab'))
remDr$sendKeysToActiveElement(list(key = 'tab'))
remDr$sendKeysToActiveElement(list(key = 'tab'))
remDr$sendKeysToActiveElement(list(key = 'tab'))
remDr$sendKeysToActiveElement(list(key = 'enter'))
`

Notice that I decided to use tab and not the down arrow. The down arrow was not working for some reason. I hope this helps someone!