-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Can't get the sected options of a <select> #161
Comments
Hi @Yvem yeah it is possible. Lets say we have the following <select name="test" id="select">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select> You get the current selected option by using var webdriverjs = require('webdriverjs'),
client = webdriverjs.remote({desiredCapabilities:{browserName:'phantomjs'}}).init();
client
.url('http://localhost:8080')
.getValue('#select',function(err,val){
console.log(val); // will output "1"
})
.click('//*[@id="select"]/option[3]')
.getValue('#select',function(err,val){
console.log(val); // will output "3"
})
.end(); Hope that helps. cheers |
Thanks for quick answer and for the S/O question, too. |
@christian-bromann thanks ! |
is there any way to select with xpath? I was kinda thinking of getting all "options" elements and loop it through each one with getValue and check with the value I want to click, and click on the element. Am I going wrong path? All I'm concern is how to I get hold of all child elements of select element. |
Try something like this: // first fetch all option elements
client.elements('#select > option', function(err, res) {
// iterate through all option elements
res.value.forEach(function(elem) {
// get the value of each option element
client.elementIdAttribute(elem, 'value', function(err,res) {
// output value
console.log(res.value);
});
});
}) |
Thanks! will give a spin.. |
Even without command = (label, value, callback) ->
selectPath = "//select[@id=string(//label[.='#{label}']/@for)]"
@click selectPath
@wclick "#{selectPath}/option[.='#{value}']"
@call callback
browser.addCommand 'selectOption', command First thanks to my xpath selector I find my select dom element and click on it. As the select is now open, I can click on an item with text equals to |
@christian-bromann brother, it throw me an error, TypeError: Cannot read property 'value' of null also I don't have 'id' to select the element, and there are multiple select elements on the page, can't only depend on value attribute. Is there any method to get all the chlid nodes of the selector. |
Could you provide the whole stacktrace? |
20:45:33.487 INFO - Done: [new session: Capabilities [{platform=ANY, javascriptEnabled=true, browserName=chrome, version=}]] |
and the WebdriverIO one? |
you meant? terminal console? |
run your script with `logLevel: verbose' in your webdriverio options |
Selenium 2.0/webdriver protocol bindings implementation with helper commands in nodejs. For a complete list of commands, visit http://webdriver.io/docs.html.[20:53:46]: COMMAND POST {"protocol":"http:","slashes":true,"auth":null,"host":"127.0.0.1:4444","port":"4444","hostname":"127.0.0.1","hash":null,"search":null,"query":null,"pathname":"/wd/hub/session","path":"/wd/hub/session","href":"http://127.0.0.1:4444/wd/hub/session"} |
Fiddled with this for hours until it worked - Nothing obvious about it! let elements = $("#selectId").elements("option").value;
let selectedText = "";
elements.map(function(option) {
let selected = option.getAttribute("selected");
if (selected) {
selectedText = option.getText().trim();
console.log("Current selectedText is: " + selectedText);
}
});
return selectedText; |
I like using the value to retrieve the text: /** assume you found your select and called it 'element' */
/** I want to find the selected option text and store it in 'optionText' */
/**
(obviously you'd not want to clutter your namespace with 'optionValue' 'option'
so in real life you'd encapsulate this or one-line it.
I just think this is easy to read)
*/
const optionValue = element.getValue();
const option = element.$(`[value='${optionValue}']`);
const optionText = option.getText(); |
Sorry, I tried
.element
,.elements
,.getSelected
with no success.Java and C# drivers have a getFirstSelected()
Is it possible to get the selected options with webdriver.js ? If not, should it be added ?
Note : S/O question : http://stackoverflow.com/q/22153812/587407
The text was updated successfully, but these errors were encountered: