【问题标题】:How to select an item from menu-popup with selenium?如何从带有硒的菜单弹出窗口中选择一个项目?
【发布时间】:2019-06-12 11:34:25
【问题描述】:

如何从menu-popup 中选择一个元素? 比如我要选Mr.

这是网站代码:

<div class="menu-popup-items"><span class="menu-popup-item menu-popup-no-icon "><span class="menu-popup-item-icon"></span><span class="menu-popup-item-text">Not selected</span></span><span class="menu-popup-item menu-popup-no-icon "><span class="menu-popup-item-icon"></span><span class="menu-popup-item-text">**Mr.**</span></span><span class="menu-popup-item menu-popup-no-icon "><span class="menu-popup-item-icon"></span><span class="menu-popup-item-text">Mrs.</span></span><span class="menu-popup-item menu-popup-no-icon "><span class="menu-popup-item-icon"></span><span class="menu-popup-item-text">Ms.</span></span><span class="menu-popup-item menu-popup-no-icon "><span class="menu-popup-item-icon"></span><span class="menu-popup-item-text">Dr.</span></span></div>

【问题讨论】:

  • 您的代码试用版?

标签: selenium junit4 jmenupopup


【解决方案1】:

我会用我的例子来解释

      //In Page Object File
      public static WebElement idProof(WebDriver driver)
        {
            WebElement element=null;
            WebDriverWait wait=new WebDriverWait(driver, 50);
            element=wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@class='panel-body']//div[3]//div[2]//div[1]//a[1]//span[1]")));
            return element;
        }

     ` `//In Test File
      WebElement idProof = Page1.idProof(driver);
      idProof.click();

       //In Test File
       WebElement voterId = FarmerRegPage.idProofVoterId(driver, "Voter id");
       voterId.click();

  // In Page Object File
  public static WebElement idProofVoterId(WebDriver driver, String idVal)
{
    WebElement element=null;
    WebDriverWait wait=new WebDriverWait(driver, 50);
    element=wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//li[contains(text(),'" + idVal +"')]")));
    return element;
}

变量String idVal 是我在下拉列表中传递的值

HTML 片段:- &lt;span&gt;Select an Option&lt;/span&gt;

我们有同样的情况,我想告诉你,你应该先点击下拉菜单,然后你只需将 xpath span 更改为 li 并保持原样,发送你拥有的项目的名称选择它应该可以正常工作

【讨论】:

  • 你为什么要把span改成li
【解决方案2】:

我认为你应该试试这个 XPath

"//*[@class='menu-popup-items']" 

它将帮助您将每个元素定位到您的 div 标签中

如果您正在寻找特定的文本,那么它将帮助您在菜单弹出窗口中找到一个元素

 //*[contains(text(),'Mr.')]

它会在弹出菜单中找到你的先生

【讨论】:

    【解决方案3】:

    在回答问题之前,我想指出,如果您可以为每个菜单项添加一个 id,它将使查找方式更容易且性能更高。

    在这种情况下,您可以这样做:

    WebElement result = driver.findElement(By.id("myId"));
    

    如果您无法添加 id,您可以执行以下操作:

        WebElement result = driver.findElements(By.className("menu-popup-item-text")).stream()
                    .filter(webElement -> webElement.getText().contains("Mr."))
                    .findFirst().get();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多