0

I have a select box which has 3 values in the drop-down. When I click on the select box and then further use click() on the value item , it works fine. but when I use Keys.ARROW_DOWN its displays the select values but does not click/select the item in drop down. Also I don't want to use 'Select' class for it.

driver.findElement(By.xpath(".//*[@id='ctl04_Pane2_AttributeDefinitionCategoryTest']/td[2]//td[2]")).click();

        Thread.sleep(2000);

        driver.findElement(By.xpath(".//*[@id='ctl04_Pane2_AttributeDefinitionCategoryTest']/td[2]/div/select")).sendKeys(Keys.ARROW_DOWN);

Also if the 'Id' of the select box changes dynamically, then should we use the webdriver 'Select' class for it?

1

You could use

 driver.findElement(By.xpath(".//*[@id='ctl04_Pane2_AttributeDefinitionCategoryTest']/td[2]/div/select")).sendKeys(Keys.RETURN);

for an easy fix. However there are other ways to select by the value.

    Select select = new Select(driver.findElement(By.xpath(".//*[@id='ctl04_Pane2_AttributeDefinitionCategoryTest']/td[2]/div/select")));
    select.selectByVisibleText("Insert Text Here");
| improve this answer | |
  • The Select class makes it very easy to use multi-option select boxes. It has two useful methods, selectByVisibleText(String) and selectByIndex(int) which will let you respectively select by it's visible text and order in the list. Here are the javadocs for this class selenium.googlecode.com/git/docs/api/java/index.html?org/openqa/… – Julian Aug 12 '15 at 15:07
0

use driver.switchto().activeelement() before sending the arrow keys.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.