selenium
  • selenium-webdriver
  • 2017-10-05 111 views 0 likes 
    0

    我想从硒webdriver的下拉菜单中选择一个值,但它只是打开下拉菜单并立即关闭它。使用硒webdriver从下拉菜单中选择数值

    这里是webdriver的代码:

    driver.findElement(By.cssSelector("span.k-select ")).click(); 
         Thread.sleep(3000); 
         driver.findElement(By.xpath("//li[@class='k-item'][.='Revenue per click']")).click(); 
    

    这是从浏览器的下拉列表中的html代码:

    <div class="controls"> 
        <span class="k-widget k-dropdown k-header kendo-input span8 ddFix" unselectable="on"> 
         <span unselectable="on" class="k-dropdown-wrap k-state-default"> 
         <span unselectable="on" class="k-input">Select revenue type... 
         </span> 
         <span unselectable="on" class="k-select"> 
          <span unselectable="on" class="k-icon k-i-arrow-s">select</span> 
         </span> 
         </span> 
         <input name="revenueType" id="revenueType" class="kendo-input span8 ddFix" data-role="dropdownlist" style="display: none;"> 
        </span> 
    </div> 
    
    <div class="k-animation-container km-popup"> 
         <div class="k-list-container k-popup k-group k-reset" id="revenueType-list" data-role="popup"> 
          <ul unselectable="on" class="k-list k-reset" tabindex="-1" role="listbox" aria-hidden="true" id="revenueType_listbox" aria-live="off" style="overflow: auto;"> 
           <li tabindex="-1" role="option" unselectable="on" class="k-item">Select revenue type...</li> 
           <li tabindex="-1" role="option" unselectable="on" class="k-item">Revenue per action</li> 
           <li tabindex="-1" role="option" unselectable="on" class="k-item">Revenue per click</li> 
           <li tabindex="-1" role="option" unselectable="on" class="k-item">Revenue per sale</li> 
           <li tabindex="-1" role="option" unselectable="on" class="k-item">Revenue per action + Revenue per sale</li> 
           <li tabindex="-1" role="option" unselectable="on" class="k-item">Revenue per mile</li> 
          </ul> 
         </div> 
        </div> 
    
    +0

    哪里下拉 – iamsankalp89

    回答

    0

    很难没有上下文来调试,但:

    1. 您的定位器无效,请尝试使用:

      By.xpath("//li[text()='Revenue per click']") 
      

      由于只有一个项目具有该文本,所以应该足够了。如果你仍然想在选择@class,使用

      By.xpath("//li[@class='k-item' and text()='Revenue per click']") 
      
    2. 尝试使用JavaScript改变不可选择为关闭使用JavaScript执行。

    +0

    它仍然无法正常工作,因为我可以看到的主要问题是,之后点击下拉它打开并立即关闭,这第二行没有时间找到我想要选择的文本,我试图把第一次点击后Thread.sleep(3000),但它并没有帮助 – alonso05

    0

    获取下拉文本值后进行点击操作

    subdropdown_child = navigationDriver.findElements(By.cssSelector("select#ctl00_SPWebPartManager1_g_c873566a_540c_4681 option");         
    for (Element subchild : subdropdown_child) 
        {  
        dropdown_value = subchild.text(); 
        new Select(navigationDriver.findElementByCssSelector("select#ctl00_SPWebPartManager1_g_c873566a_540c_4681_")).selectByVisibleText(dropdown_value); 
        } 
    
    相关问题