如何使用Java的Selenium WebDriver选择ul中的li。

huangapple go评论98阅读模式
英文:

how to select li of ul in selenium webdriver using java

问题

以下是已翻译的内容:

我是新手使用selenium。我正在使用selenium webdriver。我想要从ul的li中选择一个值。我需要从列表项中选择一个国家。

这是HTML代码;

<div class="select-wrapper initialized">
    <span class="caret">▼</span>
    <input type="text" class="select-dropdown" readonly="true" data-activates="select-options-45625784-059c-3523-a00e-3ad824fad64a" value="-- Select --">

    <ul id="select-options-45625784-059c-3523-a00e-3ad824fad64a" class="dropdown-content select-dropdown ">
        <li class=""><span>-- Select --</span></li>
        <li class=""><span>Afghanistan</span></li>
        <li class=""><span>Canada</span></li>
    </ul>

    <select ng-if="!form.translate" id="countryCode" materializecss-select="" ng-model="model['countryCode']" ng-model-options="form.ngModelOptions" ng-disabled="form.readonly" sf-changed="form" class="initialized" schema-validate="form" ng-options="item.value as item.name group by item.group for item in form.titleMap" name="countryCode">
        <option label="-- Select --" value="string:" selected="selected">-- Select --</option>
        <option label="Afghanistan" value="string:AF">Afghanistan</option>
        <option label="Canada" value="string:AF">Canada</option>
    </select>
</div>

这是我的selectCountry函数。

By countryInput = By.xpath("//[@id=\"countryCode_inputfileddiv\"]/div/input");

public void selectCountry() {
    WebElement dropdown = driver.findElement(countryInput);
    dropdown.click();

    List<WebElement> options = driver.findElements(By.id("select-options-b9a0d236-4008-830c-5319-9405f74b872f"));

    for (WebElement option : options) {
        String valueToSelect = option.getText().trim();
        System.out.println(valueToSelect);
        if (valueToSelect.equals("Canada")) {
            continue;
        }

        option.click();
    }
}

如何点击下拉框?

我尝试了上面的示例行,但出现错误:

元素当前不可见,因此可能无法进行交互操作 命令持续时间

英文:

I am new to selenium. I am working on selenium webdriver. i want to select a value from the li of ul. I need to select a country from list items.

Here is the HTML code;

&lt;div class=&quot;select-wrapper initialized&quot;&gt;
&lt;span class=&quot;caret&quot;&gt;▼&lt;/span&gt;
&lt;input type=&quot;text&quot; class=&quot;select-dropdown&quot; readonly=&quot;true&quot; data-activates=&quot;select-options-45625784-059c-3523-a00e-3ad824fad64a&quot; value=&quot;-- Select --&quot;&gt;

&lt;ul id=&quot;select-options-45625784-059c-3523-a00e-3ad824fad64a&quot; class=&quot;dropdown-content select-dropdown &quot;&gt;

&lt;li class=&quot;&quot;&gt;&lt;span&gt;-- Select --&lt;/span&gt;

&lt;/li&gt;&lt;li class=&quot;&quot;&gt;&lt;span&gt;Afghanistan&lt;/span&gt;&lt;/li&gt;
&lt;/li&gt;&lt;li class=&quot;&quot;&gt;&lt;span&gt;Canada&lt;/span&gt;&lt;/li&gt;

&lt;/ul&gt;

&lt;select ng-if=&quot;!form.translate&quot; id=&quot;countryCode&quot; materializecss-select=&quot;&quot; ng-model=&quot;model[&#39;countryCode&#39;]&quot; ng-model-options=&quot;form.ngModelOptions&quot; ng-disabled=&quot;form.readonly&quot; sf-changed=&quot;form&quot; class=&quot;initialized&quot; schema-validate=&quot;form&quot; ng-options=&quot;item.value as item.name group by item.group for item in form.titleMap&quot; name=&quot;countryCode&quot;&gt;

&lt;option label=&quot;-- Select --&quot; value=&quot;string:&quot; selected=&quot;selected&quot;&gt;-- Select --&lt;/option&gt;

&lt;option label=&quot;Afghanistan&quot; value=&quot;string:AF&quot;&gt;Afghanistan&lt;/option&gt;
&lt;option label=&quot;Canada&quot; value=&quot;string:AF&quot;&gt;Canada&lt;/option&gt;

&lt;/select&gt;

&lt;/div&gt;

Here is my selectCountry function.

By countryInput = By.xpath(&quot;//*[@id=\&quot;countryCode_inputfileddiv\&quot;]/div/input&quot;);

public void selectCountry() {
        WebElement dropdown = driver.findElement(countryInput);
        dropdown.click();

        List&lt;WebElement&gt; options = driver.findElements(By.id(&quot;select-options-b9a0d236-4008-830c-5319-9405f74b872f&quot;));

        for (WebElement option : options) {
            String valueToSelect = option.getText().trim();
            System.out.println(valueToSelect);
            if (valueToSelect.equals(&quot;Canada&quot;)) {
                continue;
            }

            option.click();
        }

    }

How to click the drop down?

I tried with the above example lines but am getting error such as

> Element is not currently visible and so may not be interacted with Command duration

答案1

得分: 1

我在HTML中看到一个<select>节点。你可以使用selenium的Select类来处理/选择下拉框的值。参考下面的代码:

Select drpCountry = new Select(driver.findElement(By.id("countryCode")));
drpCountry.selectByVisibleText("Canada");

所需的导入:

import org.openqa.selenium.support.ui.Select;
英文:

I see a &lt;select&gt; node in the HTML. You can use selenium's Select class to handle/choose dropdown values. Refer the code below:

Select drpCountry = new Select(driver.findElement(By.id(&quot;countryCode&quot;)));
drpCountry.selectByVisibleText(&quot;Canada&quot;);

Imports required:

import org.openqa.selenium.support.ui.Select;

答案2

得分: 0

public void selectCountry() {
WebElement dropdown = driver.findElement(By.id("countryCode"));
Select select = new Select(dropdown);
select.selectByVisibleText("Afghanistan");
}

if we want to select Canada just pass Canada in selectByVisibleText() method

英文:
    public void selectCountry() {
    	WebElement dropdown = driver.findElement(By.id(&quot;countryCode&quot;));
    	Select select=new Select(dropdown);
    	select.selectByVisibleText(&quot;Afghanistan&quot;);
    }

if we want to select Canada just pass Canada in selectByVisibleText() method

huangapple
  • 本文由 发表于 2023年7月20日 16:51:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76728188.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定