0

I have currently hit a stumbling block when trying to simply pick an option from a dropdown menu whilst automating a website.

Here is a snippet of the HTML:

<select id="alarm-dropdown" name="alarm-dropdown" data-bind="value: AlarmCode" data-ga-category="CarInsurance_YourVehicle_VehicleDetails_FittedWithAnAlarm" data-ga-action="Selected" data-ga-label="CarInsurance_YourVehicle_VehicleDetails_FittedWithAnAlarm">
   <option class="" selected="" disabled="" value="">Please select...</option>
   <option class="" id="alarm-dropdown-99991" value="99991">Factory Fitted Thatcham Approved Alarm/Immobiliser</option>
   <option class="" id="alarm-dropdown-99992" value="99992">Factory Fitted Thatcham Approved Alarm</option>
   <option class="" id="alarm-dropdown-99993" value="99993">Factory Fitted Non-Thatcham Alarm/Immobiliser</option>
   <option class="" id="alarm-dropdown-99994" value="99994">Factory Fitted Non-Thatcham Alarm</option>
   <option class="" id="alarm-dropdown-#F" value="#F">Factory Fitted</option>
   <option class="" id="alarm-dropdown-#N" value="#N">None</option>
</select>

Here is my current code: -

    Select select = new Select(driver.findElement(By.id("alarm-dropdown")));
    select.selectByValue("Factory Fitted Non-Thatcham Alarm");

I have tried XPATH/ID/CSS

5
  • 1
    I would think you need to use a value in the selectByValue() method - you are giving it the text instead. Instead of Factory Fitted Non-Thatcham Alarm use 99994. Commented Oct 29, 2017 at 16:44
  • Thanks for reply... Yea, I tried that originally as well.. I think its when im trying to identify the top level dropdown box... So strange... I dont seem to be able to "Get to" the available options in the dropdown... Driving me mad! Cheers Steve Commented Oct 29, 2017 at 17:02
  • Are you getting any errors like NoSuchElementException etc? Commented Oct 29, 2017 at 17:16
  • Yea best I have got in the console is: - Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element: .\/html\/body\/div[3]\/div[2]\/article\/div[4]\/section\/section[1]\/div\/ol\/li[4]\/ol\/li[2]\/label[1]\/span[1] For documentation on this error, please visit: seleniumhq.org/exceptions/no_such_element.html Commented Oct 29, 2017 at 17:22
  • I have resolved it.... I really dont like using the Xpath but in this case it seems the only viable option... Please see attached code: - element = driver.findElement(By.xpath("//*[@id=\"alarm-dropdown\"]")); element.click(); Select select = new Select(driver.findElement(By.id("alarm-dropdown"))); select.selectByValue("#F"); Thanks guys Steve Commented Oct 29, 2017 at 17:30

2 Answers 2

1
Select select = new Select(driver.findElement(By.id("alarm-dropdown")));
select.selectByValue("99994");
Sign up to request clarification or add additional context in comments.

Comments

0

Try with the xpath:

driver.findElement(By.xpath("//select[@id='alarm-dropdown']/option[@value='99994']")).click();

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.