11

I have a select control on my site. I am using page objects to interact with the page. If I do (with the first 2 lines under my class and the selectByValue in my method)

@FindBy(id="foo")
private Select foo;

foo.selectByValue("myValue");

It fails with a null pointer. I also tried without the @FindBy.

Now if I do this in my method it all works fine and selects the correct item

Select foo = new Select(sDriver.findElement(By.id("foo")));
foo.selectByValue("myValue");

Here is the actual web snippet for that control (edited to protect the innocent)

<select id="foo" name="service_name">
    <option selected="selected" value="one">one</option>
    <option value="two">two</option>
    <option value="three">three</option>
</select>

Let me say that I have a work around for my issue but I don't get why the "normal" path is not working.

zephryl
  • 14,633
  • 3
  • 11
  • 30
ducati1212
  • 855
  • 9
  • 18
  • 29

3 Answers3

25

Thats because the Select class has this constructor:

Select(WebElement element)

See the Javadoc

So if you do something like this:

@FindBy(id="foo")
private WebElement wannabeSelect;
Select realSelect = new Select(wannabeSelect);
realSelect.selectByValue("myValue");

It should work.

BTW, I am using the same approach as you in the "workaround" because I dont wanna cast new WebElement object when I need Select object. But anyways, the

sDriver.findElement(By.id("foo"));

returns WebElement, so thats why its working. You can also do this:

 WebElement wannabeSelect = sDriver.findElement(By.id("foo"));
 Select foo = new Select(wannabeSelect);
Luis Perez
  • 27,650
  • 10
  • 79
  • 80
Pavel Janicek
  • 14,128
  • 14
  • 53
  • 77
  • ahh that makes sense it just seemed like it should have worked so I never really dug to deep. My fault. Thank you – ducati1212 Mar 07 '12 at 15:48
  • Note: if you include the Select declaration and cast in your class, just below the private WebElement declaration, you will get runtime errors when the class is instantiated; to avoid this, you can put the Select declaration and class in the method which does the interaction with the screen control. Again not ideal, but it does seem to work. – Vince Bowdren Apr 30 '13 at 10:53
  • with this approach, the pageobjects should be initialized after the page has fully loaded and that the object is present and visible in the driver. To have it proxied, take a look at [my implementation](http://stackoverflow.com/questions/35931667/select-object-on-seleniums-pagefactory/36004572#36004572) – rrw Mar 17 '16 at 00:45
2

There are two ways to select the option value:

One:

// Denotes option value - technical name
select.selectByValue(fieldValue);       

Two:

// Denotes option text that is actually visible to be selected
select.selectByVisibleText(fieldValue);
manuell
  • 7,528
  • 5
  • 31
  • 58
Ajith Moni
  • 31
  • 1
1

Other way I achieved this is by using below method for all my onchange dropdownselection boxes. Pass id and selection and it works

public void onchangedropdownselection(String object, String value) {
        WebElement obj = driver.findElement(By.id(object));
        obj.sendKeys(value);
        obj.sendKeys(Keys.UP);
        obj.sendKeys(Keys.DOWN);
    }

By doing up and down we are initialzing the script onchange.......

rrw
  • 671
  • 7
  • 18
Satish
  • 11
  • 4