[java] Selenium 2에서 드롭 다운 옵션을 선택 / 가져 오는 방법

셀레늄 1 코드를 셀레늄 2로 변환하고 있는데 드롭 다운 메뉴에서 레이블을 선택하거나 드롭 다운에서 선택한 값을 얻는 쉬운 방법을 찾을 수 없습니다. Selenium 2에서 어떻게하는지 알고 있습니까?

다음은 Selenium 1에서는 작동하지만 2에서는 작동하지 않는 두 가지 진술입니다.

browser.select("//path_to_drop_down", "Value1");
browser.getSelectedValue("//path_to_drop_down");



답변

셀레늄 문서에서 webdriver를 사용하여 양식채우는 방법Select 클래스에 대한 javadoc에 대한 섹션을 살펴보십시오 .

레이블을 기준으로 옵션을 선택하려면 :

Select select = new Select(driver.findElement(By.xpath("//path_to_drop_down")));
select.deselectAll();
select.selectByVisibleText("Value1");

처음 선택한 값을 얻으려면 :

WebElement option = select.getFirstSelectedOption()


답변

driver.findElement(By.id("id_dropdown_menu")).click();
driver.findElement(By.xpath("xpath_from_seleniumIDE")).click();

행운을 빕니다


답변

지속적으로 사용하기 위해 루비에서 다음을 추가하십시오.

module Selenium
  module WebDriver
    class Element
      def select(value)
        self.find_elements(:tag_name => "option").find do |option|
          if option.text == value
            option.click
              return
           end
       end
    end
  end
end

값을 선택할 수 있습니다.

browser.find_element(:xpath, ".//xpath").select("Value")


답변

다음을 사용해보십시오.

selenium.select("id=items","label=engineering")

또는

selenium.select("id=items","index=3")


답변

janderson이 위에 게시 한 것과 유사한 옵션은 셀레늄 2에서 .GetAttribute 메서드를 사용하는 것입니다.이를 사용하면 찾고있는 특정 값이나 레이블이있는 모든 항목을 가져올 수 있습니다. 이는 요소에 레이블, 스타일, 값 등이 있는지 확인하는 데 사용할 수 있습니다.이를 수행하는 일반적인 방법은 원하는 항목을 찾아 선택할 때까지 드롭 다운의 항목을 반복하는 것입니다. C #에서

int items = driver.FindElement(By.XPath("//path_to_drop_Down")).Count();
for(int i = 1; i <= items; i++)
{
    string value = driver.FindElement(By.XPath("//path_to_drop_Down/option["+i+"]")).GetAttribute("Value1");
    if(value.Conatains("Label_I_am_Looking_for"))
    {
        driver.FindElement(By.XPath("//path_to_drop_Down/option["+i+"]")).Click();
        //Clicked on the index of the that has your label / value
    }
}


답변

다음과 같이 할 수 있습니다.

public void selectDropDownValue(String ValueToSelect)
{

    webelement findDropDownValue=driver.findElements(By.id("id1"))    //this will find that dropdown 

    wait.until(ExpectedConditions.visibilityOf(findDropDownValue));    // wait till that dropdown appear

    super.highlightElement(findDropDownValue);   // highlight that dropdown     

    new Select(findDropDownValue).selectByValue(ValueToSelect);    //select that option which u had passed as argument
}


답변

이 메서드는 드롭 다운에 대해 선택한 값을 반환합니다.

public static String getSelected_visibleText(WebDriver driver, String elementType, String value)
  {
    WebElement element = Webelement_Finder.webElement_Finder(driver, elementType, value);
   Select Selector = new Select(element);
    Selector.getFirstSelectedOption();
    String textval=Selector.getFirstSelectedOption().getText();
    return textval;
  }

그 동안에

문자열 textval = Selector.getFirstSelectedOption ();

element.getText ();

드롭 다운의 모든 요소를 ​​반환합니다.