How Can i select value "bb" from the dropdown without using XPath. The code is
First Dropdown
<div class="col-sm-4 col-md-4 col-lg-4">
<div class="form-label-group">
<select class="custom-select">
<option value="" selected="selected">-- Select --</option>
<option value="09">
dd
</option>
<option value="08">
ee
</option>
<option value="07">
ff
</option>
</select>
<label for="Teacher">person</label>
</div>
</div>
Second DropDown
<div class="col-sm-4 col-md-4 col-lg-4">
<div class="form-label-group">
<select class="custom-select">
<option value="" selected="selected">-- Select --</option>
<option value="01">
aa
</option>
<option value="02">
bb
</option>
<option value="03">
cc
</option>
</select>
<label for="profile">student</label>
</div>
</div>
I want to select value bb from second dropdown. When code runs it goes to first dropdown and cannot find value bb and fails. How can I do that?
Dom contains two dropdown with same class but different label so we can differ the dropdowns using the label
Xpath for label Teacher: //label[@for="Teacher"]//ancestor::div//select
Xpath for label profile: //label[@for="profile"]//ancestor::div//select
Now You can use select class for the dropdown's
var student_drpdwn= driver.FindElement(By.xpath("//label[@for="Teacher"]//ancestor::div//select"));
var person_drpdwn= driver.FindElement(By.xpath("//label[@for="profile"]//ancestor::div//select"));
//Initialize class select with student_drpdwn webelement object
var select_person=new Select(person_drpdwn);
var select_student=new Select(student_drpdwn);
//select by value
select_person.selectByValue("09");
select_student.selectByValue("01");
// select by text
select_person.SelectByText("dd");
select_student.SelectByText("aa");
Identify your drop down By Xpath:
var student = driver.FindElement(By.XPath("//label[text()='student']//ancestor::div//select"))
By CSS Selector:
var student = driver.FindElement(By.CssSelector(".custom-select:nth-of-type(2)")) # As student is second drop down on page with class name as custom-select
Now, create select type
var selectStudent = new SelectElement(student);
//select by value
selectStudent.SelectByValue("02");
// select by text
selectStudent.SelectByText("bb");
//Select By Index. Index start at 0, so aa-0, bb-1
selectStudent.SelectByIndex(1)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.