Advertisement

✔ (83) Select Values: How To Handle Drop Downs

✔ (83) Select Values: How To Handle Drop Downs Download Presentation, Code, & Transcript
WebPage:
GitHub:

► Free Selenium PDF Book:
► Free Java PDF Book:
► All Paperback & eBooks:

Transcript
Hello and Welcome, in this video series, we are going to look at How To Handle Drop Downs. You may hear drop-down menu, drop-down list, or drop-down box. Either name is okay. First is selecting values from a drop-down, then getting values from a drop-down, and last is deselecting values from a drop-down. Drop-downs are common elements but they are not all created the same.

Most drop-downs contain a select tag name. However, there are cases when the drop-down list may not contain a select tag name. For example, when I inspect this second element. It contains a span tag name.

This series will focus on the select tagName although a drop-down can be other tag names. Let’s start with selecting values from a drop-down.

Select Values From A Drop Down
Our 1st application is OrangeHRM. We are going to select options from Job Title, Sub Unit, and Include. Inspect the drop-down for Job Title and we see the select tag with an id value of empsearch_job_title.

Go to Eclipse. Selenium has a class called Select that helps with selecting options from a drop-down. There are 2 ways to find a drop-down and select an element from the drop-down. The 1st way is to find the WebElement then add the Select class that takes the WebElement. The 2nd way is to start with the Select class then add the WebElement. I will use the 1st way on Job Title then use the 2nd way on Sub Unit and Include.

driver.findElement(By.id("empsearch_job_title")); then assign it to a WebElement and write the object of findJobTitle. Select is the class provided by Selenium, selectJobTitle is the object new Select (). new Select is a constructor that receives a WebElement. Hover over the error and it states “The constructor is undefined”. It’s undefined because we have not added findJobTitle as the WebElement. The purpose of a Select constructor is to make sure the element is located in a select tag. If it is not located in a select tag then an UnexpectedTagNameException is thrown.

selectByIndex
Now, let’s select an option from the Job Title drop down. Write selectJobTitle. and we see a lot of methods. However, there are 3 methods for selecting an option. Let’s write select and the methods are selectByIndex, selectByValue, and selectByVisibleText. Start with selectByIndex which Select the option at the given index. All indexes begin with 0.

Go back to the AUT and inspect the options. Index 0 is All, index 1 is Account Clerk, and index 2 is CEO. Let’s select index 1 which is Account Clerk. Go back to the Eclipse IDE. selectJobTitle.selectByIndex(1); Select Account Clerk

selectByValue
The next drop-down is Sub Unit. Write Select selectSubUnit = new Select (). Now, let’s go to the AUT and get the WebElement. id is empsearch_sub_unit. Let’s look at the options. We see values 0, 4, 5, 6, 7. Let’s use 6 for IT. Go back to Eclipse and write.

driver.findElement(By.id("empsearch_sub_unit"))); The next method will be selectByValue so we write selectSubUnit.selectByValue. It “select all options that have a value matching the argument”. Notice the parameter is a String. Therefore, we write 6 in double quotes. // Select IT

selectByVisibleText
The last drop-down is Include. Inspect and the id is empsearch_termination. Look how the drop down shows the same information between the option tags. This is the visible text. We are going to use Past Employees Only. Go to Eclipse

Select selectInclude = new Select (driver.findElement(By.id("empsearch_termination")));
selectInclude.selectByVisibleText. This description states “Select all options that display text matching the argument”. Past Employees Only. We should see Account Clerk, IT, and Past Employees Only in the drop downs after running this program. All of the expected values are selected on the AUT. That’s it for selecting a value from a drop-down list.

Next, is getting values from a drop-down.

#SelectValuesFromDropDown #HowToHandleDropDown #BeginnerSeleniumTutorials #SeleniumWebDriver

Selenium Tutorial,Selenium Training,Selenium Testing Tutorial,Selenium Online Training,Selenium Introduction,Selenium Frameworks,Learn Selenium,Selenium Testing Tool,Selenium Tutorial For Beginners,Selenium Automation,Test Automation,Selenium Basics,Selenium Step By Step,Selenium Components,Selenium Tips,Selenium Overview,Selenium Fundamentals,Selenium 4 Beginners,Selenium For Beginners,

Post a Comment

0 Comments