johntitus / node-horseman

Run PhantomJS from Node
MIT License
1.45k stars 124 forks source link

Select option without value #299

Open tp6g04jp6 opened 7 years ago

tp6g04jp6 commented 7 years ago

How to select an option without value?

For example: HTML:

<select name="main_road" id="main_road" onchange="mainRoadChange();">
<option>A</option>
<option>B</option>
<option>C</option>
</select>
yurydyatlov commented 7 years ago

No answer? Also need help with this.

awlayton commented 7 years ago

Doesn't this work?

horseman.select('#main_road', 'A') // Select first option
yurydyatlov commented 7 years ago

Could not succeed. Can i select by index?

awlayton commented 7 years ago

No you cannot select by index.

It is weird to me that the option tags do not have a value. The default for value is supposed to be the text inside the option tag. If that does not work, I do not know how to select it short of something like:

horseman.evaluate(function() {
  // Select first option
  $('#main_road :nth-child(1)').prop('selected', true);
});
yurydyatlov commented 7 years ago

First of all thank you for your help. I tried your code and it should work, but i realised that not in my case, in website i'm crawling i think where is onclick event when selecting (That is why there is no value in option). So i should simulate click on option. Any chance to do this?

awlayton commented 7 years ago

You should be able to just click on it then.

horseman.click('#main_road :nth-child(1)')
yurydyatlov commented 7 years ago

Didn't work. Should i open/click Select element before click?