Here is the HTML code:
<select id="faculty_id" name="faculty_id[]" class="form-control select-select2" multiple="" tabindex="-1" style="display: none;">
<option value="1">Scholl Related matrial</option>
<option value="2">English Department</option>
<option value="3">Management Department</option>
<option value="4">Mind Channel</option>
<option value="5">Other</option>`
<option value="6">Office Department </option>
</select>
I am new to selenium , currently am working on selenium web driver. Please suggest me some ways to click the drop down. I am creating script but did not get perfect call. Please help me to create script to click the dropdown and store the value in array.
Dropdown menu screenshot: http://prntscr.com/100j0km
Dropdown menu list: http://prntscr.com/100j1og
-
is it a combox when you click that field is drop down showing up ?PDHide– PDHide2021年02月19日 21:32:58 +00:00Commented Feb 19, 2021 at 21:32
-
The example element you show here has 'style="display: none;"' meaning it is not visible. I think you are trying to interact with the wrong element.Niels van Reijmersdal– Niels van Reijmersdal2021年02月22日 10:13:20 +00:00Commented Feb 22, 2021 at 10:13
-
@MS Demo is trying to another method but Still Is not working. Html Code= prnt.sc/103kh9e Script code =prnt.sc/103kjb2 Console Output = prnt.sc/103kk2gIAmMilinPatel– IAmMilinPatel2021年02月22日 10:31:54 +00:00Commented Feb 22, 2021 at 10:31
1 Answer 1
As your element has style="display: none;"
it is not displayed. Looks like the Select is an hidden container to store that data that is trigger by an INPUT element. Try setting the value of input or sending keys to the input.
Below doesnt work, because select is not interact-able:
The Selenium WebDriver has an API for working with Selects, read the working with Selects documentation here.
For Java and your situation this would look something like:
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement selectElement = wait.until(ExpectedConditions.elementToBeClickable(By.id("faculty_id")));
Select selectObject = new Select(selectElement);
selectObject.selectByIndex(1);
selectObject.selectByValue("2");
selectObject.selectByVisibleText("English Department");
Do not try to manual click the select and getting all the items. Use the API :)
-
-
1@MSDemo You get a error? "Still not working" is not a very helpfull comment to assist you resolving an issue. We cannot see your screen, so you need to provide any information that might give us a clue what is going on on your side.Niels van Reijmersdal– Niels van Reijmersdal2021年02月19日 13:29:44 +00:00Commented Feb 19, 2021 at 13:29
-
Probably the Select is not there yet when you try to access it, try waiting for it to be visible. Or it is in a HTML iFrame, try to switch to the frame first.Niels van Reijmersdal– Niels van Reijmersdal2021年02月19日 13:30:48 +00:00Commented Feb 19, 2021 at 13:30
-
I am trying to another method but Still Is not working. Html Code= prnt.sc/103kh9e Script code =prnt.sc/103kjb2 Console Output = prnt.sc/103kk2g –MS Demo– MS Demo2021年02月22日 09:32:46 +00:00Commented Feb 22, 2021 at 9:32
-
1I got the solution, Thanks for the help.MS Demo– MS Demo2021年02月23日 06:48:11 +00:00Commented Feb 23, 2021 at 6:48