【问题标题】:How can I locate value from dropdown in C# selenium?如何从 C# selenium 的下拉列表中找到值?
【发布时间】:2020-12-03 03:08:23
【问题描述】:

如何在不使用 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 并失败。我该怎么做?

【问题讨论】:

  • 您不想使用 xpath 的任何特殊原因?正如您提到的“我如何在不使用 XPath 的情况下从下拉列表中选择值“bb””

标签: c# selenium selenium-webdriver mstest


【解决方案1】:

Dom 包含两个具有相同类别但标签不同的下拉菜单,因此我们可以使用标签来区分下拉菜单

标签教师的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");
    

【讨论】:

  • 它没有用。我已经用更多代码更新了这个问题。请看一下。
【解决方案2】:

确定您的下拉菜单 通过 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)

【讨论】:

  • 是否可以不使用XPATH for var student = driver.FindElement(By.XPath("//label[text()='student']//ancestor::div/ /选择")) ?
  • @Mehwish 您是否希望使用其他定位器而不是 xPath ?如果是,请查看更新的解决方案。我已经添加了 css 定位器。但请注意,在这种特殊情况下,xPath 是一个更好的解决方案,因为 i 不像 CSS selecor 那样依赖索引。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-07-03
  • 1970-01-01
  • 2020-08-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多