无法点击“下拉列表”中的“文字”

时间:2017-02-14 11:12:57

标签: java selenium selenium-webdriver webdriver

  1. 无法点击“下拉列表”
  2. 中的“文字”
  3. 将信息输出到控制台时,它会列出所有标题(先生,夫人......)
  4. 我只是无法点击.Mr文字,任何想法? (我不想使用发送密钥)

    public void selectOptionFromDropdownMenu(WebElement dropdown, String textToSearchFor) {
    List<WebElement> options = dropdown.findElements(By.tagName("option"));
    dropdown.click();
    System.out.println(options.size());
    
    for (int i = 0; i < options.size(); i++) {
        System.out.println("DEVIL!!!"  + options.get(i).getText());
    
        if(options.get(i).getText() == "Mr.") {
            options.get(i).click();
        }
    }
    

    }

  5. enter image description here

    <select id="titlefield" class="form-control ng-pristine ng-untouched ng-invalid ng-invalid-required" name="Salutation" ng-model="PersonalDetails.Salutation" ng-options="salut.id as salut.id for salut in Salutations" ng-required="FlowData.IsGuest" required="required">
    <option class="ng-binding" value="">Please select</option>
    <option value="0" label="Mr.">Mr.</option>
    <option value="1" label="Miss">Miss</option>
    <option value="2" label="Mrs.">Mrs.</option>
    <option value="3" label="Ms.">Ms.</option>
    <option value="4" label="Dr.">Dr.</option>
    </select>
    

3 个答案:

答案 0 :(得分:2)

public void selectOptionFromDropdownMenu(WebElement dropdown, String textToSearchFor) {
List<WebElement> options = dropdown.findElements(By.tagName("option"));
Select selectDropdown = new Select(dropdown);
//dropdown.click();
System.out.println(options.size());

for (int i = 0; i < options.size(); i++) {
    System.out.println("DEVIL!!!"  + options.get(i).getText());

    if(options.get(i).getText().equals(textToSearchFor)) {
    selectDropdown.selectByVisibleText(textToSearchFor);
        //options.get(i).click();
    }
}

答案 1 :(得分:1)

您可以在此处使用Select class。

Select oSelect = new Select(driver.findElement(By.id("<use the id>")));
List<WebElement> elem = oSelect.getOptions();
int iSize = elem.size();
for(int i=0; i <iSize;i++)
    {
        String sValue = oSelect.getOptions().get(i).getText();
        oSelect.getOptions().get(i).click();
        System.out.println(sValue);
                if(sValue.equals("Miss"))
        {
            System.out.println("Position of the value is" +i);
            oSelect.selectByIndex(i);


            break;
        }
    }

答案 2 :(得分:0)

element=//select[contains(@id,"titlefield")]/option[contains(@value,"0") and @label="Mr."]


public void javascriptclick(String element)
    { 
        WebElement webElement=driver.findElement(By.xpath(element));
        JavascriptExecutor js = (JavascriptExecutor) driver;

        js.executeScript("arguments[0].click();",webElement);   
        System.out.println("javascriptclick"+" "+ element);

    }