Skip to content
CodeKruPro

Read the option that is already selected

Automation taskEasyAsked in: Accenture, Capgemini, Infosys, SaaS Labs, TCS, Wipro

"How do you verify the default value of a dropdown?" Read the first selected option before touching the dropdown, then check both its text and its value.

Get First Selected Option

Get First Selected Option

getFirstSelectedOption() returns the currently selected option element.

On the Dropdowns page, dropdown #5 (id="preSelectedDropdown") has "JavaScript" pre-selected.

HTML Structure:

<select id="preSelectedDropdown">
  <option value="html">HTML</option>
  <option value="css">CSS</option>
  <option value="javascript" selected>JavaScript</option>
  <option value="typescript">TypeScript</option>
</select>

Note the selected attribute on the JavaScript option.

Task:

  1. Navigate to https://testkru.com/Elements/Dropdowns
  2. Get the first selected option from the preSelectedDropdown
  3. Print its text and value

Expected output:

Selected text: JavaScript
Selected value: javascript