0

I am trying to scrape this website. I got a problem with text fields because text fields and drop-down menus combined. The site does not allow to enter the names of cities directly. For example, I only want to take the port pointers (ex: Port of Shanghai, China), firstly, I must enter "Shanghai, China", and need to click the first option, then need to click "Port of Shanghai, China". (actually, ports are the second option). With the following codes, I can select the country name but not the port.

> remote_driver$findElement(using = 'id', value ='from')$sendKeysToElement(list("Shanghai, China"))$clickElement()
> remote_driver$findElement(using = "css", value = "#from-autocomplete > div > div > div > div:nth-child(1)")$clickElement()
> remote_driver$findElement(using = "css", value = "#from-autocomplete > div > div > div > div.fvlk5rSgKI--Xo4zMn3an > div:nth-child(2)")$clickElement()

But in the second selection for port names (3rd line), the port name has been erased from the text field.

Thanks in advance!

1
  • Hi, ayth and welcome to SQA stack exchange. Giving only a link to a site is not enough info. Please provide part of the HTML DOM where you element is. As far as I see from you code it looks like the classes that you want to select are dynamic and you have to find some static value to select. Also using ">" in you css selector is highly breakable so instead design your selectors to be flexible to the changes. Google for css selectors reference w3schools. PS: from the website that you've provided the first and the second options are not selectable(bug in the site) when type "Shanghai, China"
    – chesthar
    Commented May 16, 2021 at 17:52

1 Answer 1

0

Fist of all, this "drop-downs menu" are more appropriate to be called "suggestion results". So here is my answer and hope it works because I don't have RSelenium to test it. I added intentionally some sleep just to give enough time for the components to load (delete the sleeps if it works without them after you test it). I also modified the css selectors to be less breakable:

> remote_driver$findElement(using = 'id', value ='from')$sendKeysToElement(list("Shanghai, China"))$clickElement()
> Sys.sleep(2)  
> remote_driver$findElement(using = "css", value = "#from-autocomplete [tabindex="0"]")$clickElement()
> Sys.sleep(2)  
> remote_driver$findElement(using = "css", value = "#from-autocomplete [tabindex="1"]")$clickElement()
1
  • I tested but the same problem occurs again - for port selection, the text has been erased from the field. An interesting nuance is that when we run the first code, Error: attempt to apply non-function this error occurs, but the code works.
    – aytn
    Commented May 31, 2021 at 8:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.