简体   繁体   English

列表中的值与我的字符串不匹配

[英]Values from list not match with my string

I am trying to select one auto complete dropdown from the below code for unites states but below issues are observed.我正在尝试从下面的代码中为联合州选择一个自动完成下拉列表,但观察到以下问题。

  1. I have asked to select value="United States" but its selects Albania我已经要求选择 value="United States" 但它选择了阿尔巴尼亚
  2. It throws stale element exception.它抛出陈旧的元素异常。

I am not able to understand whats happening.我无法理解发生了什么。

void selectFromDropdown(WebElement webName, String valtoSelect){
        WebElement dropdown = webName;
        dropdown.click(); // assuming you have to click the "dropdown" to open it
        List<WebElement> options = dropdown.findElements(By.tagName("li"));
            for (WebElement option : options)
            {
                if (option.getText().equals(valtoSelect))
                {
                    option.click(); // click the desired option
                }
                break;
            }
        }

在此处输入图片说明

<input aria-invalid="false" autocomplete="off" id="disable-clearable" placeholder="Country of Registration" type="text" class="MuiInputBase-input MuiInput-input MuiAutocomplete-input MuiAutocomplete-inputFocused MuiInputBase-inputAdornedEnd" aria-autocomplete="list" autocapitalize="none" spellcheck="false" value="">

The problem I see is for open that auto drop down I am clicking on this element " //*[@id='disable-clearable'] "and then checking ul " //ul[@role='listbox'] "我看到的问题是打开那个自动下拉我点击这个元素“ //*[@id='disable-clearable'] ”然后检查ul“ //ul[@role='listbox']

If the options are available in the HTML you can select an item from a dropdown by three ways:如果选项在 HTML 中可用,您可以通过三种方式从下拉列表中选择一个项目:

@Test(testName = "select by tekst")
public void selectByVisibleTekst(){
    WebElement country= driver.findElement(By.xpath("locatorValue"));
    new Select(country).selectByVisibleText("tekstToSelect");
}

@Test(testName = "select by value")
public void selectByValue(){
    WebElement country= driver.findElement(By.xpath("locatorValue"));
    new Select(country).selectByValue("valueToSelect");
}

@Test(testName = "select by index")
public void selectByIndex(){
    WebElement country= driver.findElement(By.xpath("locatorValue"));
    new Select(country).selectByIndex(valueOfIndex);
}

After taking inputs from various friends here and did some reasearch and the below method worked for me.在听取了这里不同朋友的意见并进行了一些研究之后,以下方法对我有用。

public void selectCountry(){
        String valtoSelect = "United States";
        countryOfReg.click();
        List<WebElement> webMom = Collections.singletonList(driver.findElement(By.xpath("//ul[@role='listbox']")));
        for (int i = 0; i <= webMom.size(); i++) {
            System.out.println(driver.findElement(By.xpath("//*[@id='disable-clearable-option-"+i+"']")).getText());
            if (driver.findElement(By.xpath("//*[@id='disable-clearable-option-"+i+"']")).getText().equals(valtoSelect)){
                driver.findElement(By.xpath("//*[@id='disable-clearable-option-"+i+"']")).click();
                break;
            }
        }
    }

If the dropdown is really a Selection box then the following code should work如果下拉菜单真的是一个选择框,那么下面的代码应该可以工作

void selectFromDropdown(WebElement webName, String valtoSelect){
    Select select = new Select(webName)));
    select.selectByVisibleText(valtoSelect);
    // or maybe select.selectByValue(valtoSelect); depending how you setup the dropdown
}

where Select is org.openqa.selenium.support.ui.Select其中 Select 是org.openqa.selenium.support.ui.Select

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM