gaoyw2
gaoyw2

Reputation: 11

selenium selectByVisibleText not working correctly

I was trying to select the 'float' option from following select list:

<select id="pt1:tabs:ruleDictEditorTab:r2:bsets_ddc:iter:0:bse_dc:bse1:dtype::content" class="x2h" title="int" name="pt1:tabs:ruleDictEditorTab:r2:bsets_ddc:iter:0:bse_dc:bse1:dtype" _afrfoc="y1404280666174">
    <option value="0" title="String">String</option>
    <option selected="" value="1" title="int">int</option>
    <option value="2" title="double">double</option>
    <option value="3" title="char">char</option>
    <option value="4" title="byte">byte</option>
    <option value="5" title="short">short</option>
    <option value="6" title="long">long</option>
    <option value="7" title="float">float</option>
    <option value="8" title="Date">Date</option>
    <option value="9" title="Time">Time</option>
    <option value="10" title="DateTime">DateTime</option>
</select>

My selenium code is:

Select typeSelect = new Select(driver.findElement("//select"));
typeSelect.selectByVisibleText("float");

when running, I saw the select changed to 'float' for a second, but it changed back to default 'int' option immediately. Has anyone seen this kind of issue before? how to solve it?

Upvotes: 1

Views: 9555

Answers (2)

Santoshsarma
Santoshsarma

Reputation: 5667

Try with below logic

new Select(driver.findElement(By.cssSelector("select[id*='ruleDictEditorTab']"))).selectByVisibleText("float");

or

driver.findElement(By.cssSelector("select[id*='ruleDictEditorTab']")).findElement(By.cssSelector("option[title='float']")).click();

Upvotes: 0

SiKing
SiKing

Reputation: 10329

There is probably some JavaScript preventing it from changing like this. You will have to use click() to change the value. Something like this:

driver.findElement(By.xpath("//select")).click();
driver.findElement(By.xpath("//option[text()='float']")).click();

Also see this post for an alternate way of doing this.

Upvotes: 1

Related Questions