簡體   English   中英

在 selenium webdriver 中選擇下拉列表,在 div 的 ul 中選擇元素的下拉列表

[英]Selecting dropdown in selenium webdriver, dropdown list of elements in ul of div

我是 Selenium Web Driver 技術的新手,以下是我的問題,無法找到解決方案,請任何人幫我找出 x 路徑或 cs 選擇器或名稱。

下面是代碼是頁面中的下拉元素,

            <div id="reportsManager_chzn" class="chzn-container chzn-container-single chzn-container-active" style="width: 220px;">
             <a class="chzn-single" href="javascript:void(0)" tabindex="-1">
              <span>Ajay Paul Chowdhury</span>
               <div>
                <b></b>
               </div>
             </a>
            <div class="chzn-drop" style="left: -9000px; width: 218px; top: 24px;">
             <div class="chzn-search">
              <input type="text" autocomplete="off" style="width: 183px;">
            </div>
            <ul class="chzn-results">
                <li id="reportsManager_chzn_o_1" class="active-result" style="">All</li>
                <li id="reportsManager_chzn_o_2" class="active-result" style="">wer</li>
                <li id="reportsManager_chzn_o_3" class="active-result" style="">sss</li>
                <li id="reportsManager_chzn_o_4" class="active-result result-selected" style="">www</li>
                <li id="reportsManager_chzn_o_5" class="active-result" style="">rrr</li>
                <li id="reportsManager_chzn_o_6" class="active-result" style="">yyy</li>
                <li id="reportsManager_chzn_o_7" class="active-result" style="">iii</li>
                <li id="reportsManager_chzn_o_8" class="active-result" style="">ooo</li>
                <li id="reportsManager_chzn_o_9" class="active-result" style="">ppp</li>

對於上面的代碼,無法找到 xpath/cssselector/name 以便我可以從列表中動態選擇任何下拉值

請任何人幫助我找出結果。

對於上面的代碼我試過

1) 嘗試 1

    // by Xpath and it selects the list by list ID 
    
       Actions manager = new Actions(driver);
       WebElement we1=driver.findElement(By.xpath("//[@id='reportsManager_chzn_o_23']"));
      manager.moveToElement(we1).moveToElement(driver.findElement(By.xpath("//[@id='reportsManager_chzn_o_3']"))).click().build().perform();
       Thread.sleep(3000);

上面的 Selenium 代碼將選擇下拉元素“ sss ”我需要按名稱選擇的代碼,以便我可以參數化下拉列表

2)嘗試2

      driver.findElement(By.id("reportsManager_chzn")).findElement(By.cssSelector("chzn-single")).findElement(By.name("sss")).click();

顯示錯誤信息

org.openqa.selenium.NoSuchElementException: 無法定位元素:{"method":"css selector","selector":"chzn-single"}

Selenium 測試用例失敗

3) 嘗試 3

Select嘗試

   Select selectBox = new Select(driver.findElement(By.id("reportsManager_chzn")));

selectBox.selectByVisibleText("sss");

對於上面顯示的錯誤消息找到 div 而不是 select

我也一路努力,終於到了

提前致謝

對參數化 xPath 使用以下方法:

public void selector(String whichItem)
{
 String xPath_partial_1 ="//ul[@class='chzn-results']/li[reportsManager_chzn_o_"
 String xPath_parameterized = whichItem;    //This can be 1/2/3/4
 String xPath_partial_2 = "]";
 String final_xPath = xPath_partial_1 + xPath_parameterized + xPath_partial_2 ; //You can use this xPath to locate your element. Note that this xpath depends upon the number. Pass the correct number(i.e. 1 for selecting All, 2 for selecting wer and so on) to select the necessary item from the DD list
 WebElement we1=driver.findElement(By.xpath("//[@id='reportsManager_chzn_o_23']"));  //Could not locate this element in your codesnippet but assuming this is somewhere within the hierrarchy
 Actions manager = new Actions(driver);manager.moveToElement(we1).moveToElement(driver.findElement(By.xpath(final_xPath))).click().build().perform(); //Just replaced the xPath with the String. 

}

請注意,由於 DOM 中未使用 Select 標簽,因此此處不能使用 Select Class。 讓我知道它是否有幫助:)

我已經在這里發布了相同問題的答案。 對於 java 解決方案將是下一個(不確定 java 語法):

var dropDown = driver.findElement(By.Css(".chzn-results"));
//expand main dropDown menu before accessing to child elements    
dropDown.Click()
var dropDownElements = dropDown.findElements(By.Css(".active-result"));
foreach(var dropDownElement in dropDownElements)
{
    dropDownElement.Click();
}
//or your can access to element using index
dropDownElements[0].Click();

如果您想使用它的文本訪問 dropDown 元素,更好的解決方案將屬性添加到您的 li 元素(例如 li name="valueFromDropDown")

這將幫助你...

List <WebElement> allelements = driver.findElements(By.xpath("//*[@id='ui-select-choices-row-5-39']/a/span"));

  for(int i=0;i<allelements.size();i++){
  String text=allelements.get(i).getText();
    if(text.equalsIgnoreCase("Test_Region1"))
    {
    allelements.get(i).click();
    break;
    } 
    }   

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM