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?

2 Answers 2

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");
1
  • 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
    Commented Aug 12, 2015 at 15:07
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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