【问题标题】:Selecting item from dropdown从下拉列表中选择项目
【发布时间】:2018-09-03 12:45:09
【问题描述】:

我需要使用 selenium 从下拉列表中选择一个项目。该网站看起来像这样

 <div class="chosen-drop">
  <div class="chosen-search"><input type="text" autocomplete="off"></div>
  <ul class="chosen-results">
     <li class="active-result style="" data-option-array-index="0">Please choose your main activity</li>
     <li class="active-result" style="" data-option-array-index="1">Animals and Pets</li>
     <li class="active-result" style="" data-option-array-indexx="2">Art and Culture</li>
     <li class="active-result" style="" data-option-array-index="3">Babies</li>
     <li class="active-result" style="" data-option-array-index="4">Beauty and Personal Care</li>
     <li class="active-result" style="" data-option-array-index="5">Cars</li>
     <li class="active-result" style="" data-option-array-index="6">Computer Hardware and Software</li>
        </ul>

如何选择/点击“汽车”/data-option-array-index="5"?

【问题讨论】:

  • 到目前为止你尝试了什么?

标签: python selenium dropdown


【解决方案1】:

我相信您可以使用简单的 css 选择器来使用 :nth-child 获取您想要的元素。例如要获得 2 个条目,您可以编写这样的选择器:

.choosen-drop ul>li:nth-child(2)

【讨论】:

    【解决方案2】:

    你可以这样做,因为我知道 java,所以我用 java 编写代码。首先列出LIST 中的所有定位器,然后使用 For- Each 和 If else 可以选择特定项目并执行操作

    //Locate all elements in list
    List<WebElement> dropEle= driver.findElements(By.xpath("//ul[@class='chosen-results']//li"));
    for (WebElement tempEle : dropEle) {
    //condition to check text
                if(tempEle.getText().contains("Cars"))
                {
                 // Do operations
                    System.out.println("Pass");
                }
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-19
      • 1970-01-01
      • 2015-07-21
      相关资源
      最近更新 更多