如何解决如何从C#硒的下拉菜单中找到值?
如何在不使用XPath的情况下从下拉列表中选择值“ bb”。代码是
第一个下拉列表
<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>
第二个下拉列表
<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>
我想从第二个下拉列表中选择值bb。代码运行时转到第一个下拉菜单,找不到值bb并失败。我该怎么办?
解决方法
Dom包含两个具有相同类但标签不同的下拉列表,因此我们可以使用标签来区分下拉列表
标签Teacher的Xpath://label[@for="Teacher"]//ancestor::div//select
标签配置文件的Xpath://label[@for="profile"]//ancestor::div//select
现在您可以将select类用于下拉列表了
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");
,
确定您的下拉菜单 通过Xpath:
var student = driver.FindElement(By.XPath("//label[text()='student']//ancestor::div//select"))
通过CSS选择器:
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
现在,创建选择类型
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)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。