Skip to content
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

Closed
Yvem opened this issue Mar 3, 2014 · 16 comments
Closed

Can't get the sected options of a <select> #161

Yvem opened this issue Mar 3, 2014 · 16 comments

Comments

@Yvem
Copy link

Yvem commented Mar 3, 2014

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

@christian-bromann
Copy link
Member

Hi @Yvem

yeah it is possible. Lets say we have the following <select> element:

<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 getValue and you can change the selection by using click. Here is an simple example:

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

@Yvem
Copy link
Author

Yvem commented Mar 4, 2014

Thanks for quick answer and for the S/O question, too.

@sajesh2686
Copy link

@christian-bromann thanks !

@harshes53
Copy link

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.

@christian-bromann
Copy link
Member

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);
        });
    });
})

@harshes53
Copy link

Thanks! will give a spin..

@thibaut-sticky
Copy link

Even without each, it's possible

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 value.

@harshes53
Copy link

@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.

@christian-bromann
Copy link
Member

Could you provide the whole stacktrace?

@harshes53
Copy link

20:45:33.487 INFO - Done: [new session: Capabilities [{platform=ANY, javascriptEnabled=true, browserName=chrome, version=}]]
20:45:33.497 INFO - Executing: [get: http://localhost:63342/Documents/sandbox/index.html])
20:45:34.025 INFO - Done: [get: http://localhost:63342/Documents/sandbox/index.html]
20:45:34.029 INFO - Executing: [find elements: By.selector: select])
20:45:34.044 INFO - Done: [find elements: By.selector: select]
20:45:34.047 INFO - Executing: [get element attribute: [object Object] null, value])
20:45:34.047 WARN - Exception thrown
java.lang.NullPointerException
at org.openqa.selenium.remote.server.handler.GetElementAttribute.call(GetElementAttribute.java:35)
at org.openqa.selenium.remote.server.handler.GetElementAttribute.call(GetElementAttribute.java:1)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
at java.util.concurrent.FutureTask.run(FutureTask.java:138)
at org.openqa.selenium.remote.server.DefaultSession$1.run(DefaultSession.java:169)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918)
at java.lang.Thread.run(Thread.java:695)
20:45:34.047 WARN - Exception: null

@christian-bromann
Copy link
Member

and the WebdriverIO one?

@harshes53
Copy link

you meant? terminal console?

@christian-bromann
Copy link
Member

run your script with `logLevel: verbose' in your webdriverio options

@harshes53
Copy link

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"}
[20:53:46]: DATA {"desiredCapabilities":{"browserName":"chrome","version":"","javascriptEnabled":true,"platform":"ANY"}}
[20:53:47]: SET SESSION ID 90247654-14bb-4b28-a549-0fb018a23d50
[20:53:47]: RESULT {"platform":"MAC","acceptSslCerts":true,"javascriptEnabled":true,"browserName":"chrome","chrome":{"userDataDir":"/var/folders/pv/hc95_ljj13nchxvbgyzvytd4vf1j4n/T/.org.chromium.Chromium.QjPyAJ"},"rotatable":false,"locationContextEnabled":true,"webdriver.remote.sessionid":"90247654-14bb-4b28-a549-0fb018a23d50","version":"36.0.1985.125","takesHeapSnapshot":true,"cssSelectorsEnabled":true,"databaseEnabled":false,"handlesAlerts":true,"browserConnectionEnabled":false,"nativeEvents":true,"webStorageEnabled":true,"applicationCacheEnabled":false,"takesScreenshot":true}
[20:53:47]: 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/90247654-14bb-4b28-a549-0fb018a23d50/url","path":"/wd/hub/session/90247654-14bb-4b28-a549-0fb018a23d50/url","href":"http://127.0.0.1:4444/wd/hub/session/90247654-14bb-4b28-a549-0fb018a23d50/url"}
[20:53:47]: DATA {"url":"http://localhost:63342/Documents/sandbox/index.html"}
[20:53:47]: 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/90247654-14bb-4b28-a549-0fb018a23d50/elements","path":"/wd/hub/session/90247654-14bb-4b28-a549-0fb018a23d50/elements","href":"http://127.0.0.1:4444/wd/hub/session/90247654-14bb-4b28-a549-0fb018a23d50/elements"}
[20:53:47]: DATA {"using":"css selector","value":"select > option"}
[20:53:47]: RESULT [{"ELEMENT":"0"},{"ELEMENT":"1"},{"ELEMENT":"2"},{"ELEMENT":"3"},{"ELEMENT":"4"},{"ELEMENT":"5"},{"ELEMENT":"6"},{"ELEMENT":"7"},{"ELEMENT":"8"},{"ELEMENT":"9"},{"ELEMENT":"10"},{"ELEMENT":"11"}]
[20:53:47]: COMMAND GET {"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/90247654-14bb-4b28-a549-0fb018a23d50/element/[object%20Object]/attribute/value","path":"/wd/hub/session/90247654-14bb-4b28-a549-0fb018a23d50/element/[object%20Object]/attribute/value","href":"http://127.0.0.1:4444/wd/hub/session/90247654-14bb-4b28-a549-0fb018a23d50/element/[object%20Object]/attribute/value"}
[20:53:47]: DATA {}
[20:53:47]: ERROR UnknownError An unknown server-side error occurred while processing the command.
null
null
[20:53:47]: COMMAND GET {"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/90247654-14bb-4b28-a549-0fb018a23d50/element/[object%20Object]/attribute/value","path":"/wd/hub/session/90247654-14bb-4b28-a549-0fb018a23d50/element/[object%20Object]/attribute/value","href":"http://127.0.0.1:4444/wd/hub/session/90247654-14bb-4b28-a549-0fb018a23d50/element/[object%20Object]/attribute/value"}
[20:53:47]: DATA {}
[20:53:47]: ERROR UnknownError An unknown server-side error occurred while processing the command.
null
null
[20:53:47]: COMMAND GET {"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/90247654-14bb-4b28-a549-0fb018a23d50/element/[object%20Object]/attribute/value","path":"/wd/hub/session/90247654-14bb-4b28-a549-0fb018a23d50/element/[object%20Object]/attribute/value","href":"http://127.0.0.1:4444/wd/hub/session/90247654-14bb-4b28-a549-0fb018a23d50/element/[object%20Object]/attribute/value"}
[20:53:47]: DATA {}
[20:53:47]: ERROR UnknownError An unknown server-side error occurred while processing the command.
null
null
[20:53:47]: COMMAND GET {"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/90247654-14bb-4b28-a549-0fb018a23d50/element/[object%20Object]/attribute/value","path":"/wd/hub/session/90247654-14bb-4b28-a549-0fb018a23d50/element/[object%20Object]/attribute/value","href":"http://127.0.0.1:4444/wd/hub/session/90247654-14bb-4b28-a549-0fb018a23d50/element/[object%20Object]/attribute/value"}
[20:53:47]: DATA {}
[20:53:47]: ERROR UnknownError An unknown server-side error occurred while processing the command.
null
null
[20:53:47]: COMMAND GET {"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/90247654-14bb-4b28-a549-0fb018a23d50/element/[object%20Object]/attribute/value","path":"/wd/hub/session/90247654-14bb-4b28-a549-0fb018a23d50/element/[object%20Object]/attribute/value","href":"http://127.0.0.1:4444/wd/hub/session/90247654-14bb-4b28-a549-0fb018a23d50/element/[object%20Object]/attribute/value"}
[20:53:47]: DATA {}
[20:53:47]: ERROR UnknownError An unknown server-side error occurred while processing the command.
null
null
[20:53:47]: COMMAND GET {"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/90247654-14bb-4b28-a549-0fb018a23d50/element/[object%20Object]/attribute/value","path":"/wd/hub/session/90247654-14bb-4b28-a549-0fb018a23d50/element/[object%20Object]/attribute/value","href":"http://127.0.0.1:4444/wd/hub/session/90247654-14bb-4b28-a549-0fb018a23d50/element/[object%20Object]/attribute/value"}
[20:53:47]: DATA {}
[20:53:47]: ERROR UnknownError An unknown server-side error occurred while processing the command.
null
null
[20:53:47]: COMMAND GET {"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/90247654-14bb-4b28-a549-0fb018a23d50/element/[object%20Object]/attribute/value","path":"/wd/hub/session/90247654-14bb-4b28-a549-0fb018a23d50/element/[object%20Object]/attribute/value","href":"http://127.0.0.1:4444/wd/hub/session/90247654-14bb-4b28-a549-0fb018a23d50/element/[object%20Object]/attribute/value"}
[20:53:47]: DATA {}
[20:53:47]: ERROR UnknownError An unknown server-side error occurred while processing the command.
null
null
[20:53:47]: COMMAND GET {"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/90247654-14bb-4b28-a549-0fb018a23d50/element/[object%20Object]/attribute/value","path":"/wd/hub/session/90247654-14bb-4b28-a549-0fb018a23d50/element/[object%20Object]/attribute/value","href":"http://127.0.0.1:4444/wd/hub/session/90247654-14bb-4b28-a549-0fb018a23d50/element/[object%20Object]/attribute/value"}
[20:53:47]: DATA {}
[20:53:47]: ERROR UnknownError An unknown server-side error occurred while processing the command.
null
null
[20:53:47]: COMMAND GET {"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/90247654-14bb-4b28-a549-0fb018a23d50/element/[object%20Object]/attribute/value","path":"/wd/hub/session/90247654-14bb-4b28-a549-0fb018a23d50/element/[object%20Object]/attribute/value","href":"http://127.0.0.1:4444/wd/hub/session/90247654-14bb-4b28-a549-0fb018a23d50/element/[object%20Object]/attribute/value"}
[20:53:47]: DATA {}
[20:53:47]: ERROR UnknownError An unknown server-side error occurred while processing the command.
null
null
[20:53:47]: COMMAND GET {"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/90247654-14bb-4b28-a549-0fb018a23d50/element/[object%20Object]/attribute/value","path":"/wd/hub/session/90247654-14bb-4b28-a549-0fb018a23d50/element/[object%20Object]/attribute/value","href":"http://127.0.0.1:4444/wd/hub/session/90247654-14bb-4b28-a549-0fb018a23d50/element/[object%20Object]/attribute/value"}
[20:53:47]: DATA {}
[20:53:47]: ERROR UnknownError An unknown server-side error occurred while processing the command.
null
null
[20:53:47]: COMMAND GET {"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/90247654-14bb-4b28-a549-0fb018a23d50/element/[object%20Object]/attribute/value","path":"/wd/hub/session/90247654-14bb-4b28-a549-0fb018a23d50/element/[object%20Object]/attribute/value","href":"http://127.0.0.1:4444/wd/hub/session/90247654-14bb-4b28-a549-0fb018a23d50/element/[object%20Object]/attribute/value"}
[20:53:47]: DATA {}
[20:53:47]: ERROR UnknownError An unknown server-side error occurred while processing the command.
null
null
[20:53:47]: COMMAND GET {"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/90247654-14bb-4b28-a549-0fb018a23d50/element/[object%20Object]/attribute/value","path":"/wd/hub/session/90247654-14bb-4b28-a549-0fb018a23d50/element/[object%20Object]/attribute/value","href":"http://127.0.0.1:4444/wd/hub/session/90247654-14bb-4b28-a549-0fb018a23d50/element/[object%20Object]/attribute/value"}
[20:53:47]: DATA {}
[20:53:47]: ERROR UnknownError An unknown server-side error occurred while processing the command.
null
null
[20:53:47]: COMMAND DELETE {"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/90247654-14bb-4b28-a549-0fb018a23d50","path":"/wd/hub/session/90247654-14bb-4b28-a549-0fb018a23d50","href":"http://127.0.0.1:4444/wd/hub/session/90247654-14bb-4b28-a549-0fb018a23d50"}
[20:53:47]: DATA {}

@AndyGee
Copy link

AndyGee commented Oct 15, 2018

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;

@MatthiasEngh
Copy link

MatthiasEngh commented Jan 8, 2020

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();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants