代码之家  ›  专栏  ›  技术社区  ›  Jack

如何选择此类表单中的选项?

  •  0
  • Jack  · 技术社区  · 6 年前

    this

            IWebDriver driver; // assume assigned
            IWebElement ele = driver.FindElement(By.XPath("//div[title='Select your answer']"));
            ele.FindElement(By.XPath("//span[text()='Bahamas']")).Click();
    

    enter image description here

    0 回复  |  直到 6 年前
        1
  •  0
  •   Alin Stelian    6 年前

    下拉列表可以通过选择类来处理

    SelectElement oSelect = new SelectElement(driver.FindElement(By.XPath("//div[title='Select your answer']")));
    
    oSelect.selectByText("Andorra");
    

    或按索引

    //keep in mind that the index starts from 0, not from 1
    
        oSelect.SelectByIndex(3);
    

    oSelect.SelectByValue("Andorra");
    

    有关选择的更多信息,请参见 here

        2
  •  0
  •   rahul rai    6 年前

    因为它不是一个 select 无法使用的类型标记,selenium Select 方法如 byIndex , ByValue 处理此类下拉列表的最佳方法是使用 JavaScriptExecutor :

    // Click on dropdown field so that options will be available for select. Note in few cases this step wont be necessary
       driver.FindElement(By.XPath("//div[title='Select your answer']")).Click();
       IJavaScriptExecutor executor = (IJavaScriptExecutor)driver;
    //Click on option you want to select
    executor.ExecuteScript("arguments[0].click();",driver.FindElement(By.XPath("//span[text()='Bahamas']")));