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

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

how to select li of ul in selenium webdriver using java

问题

以下是已翻译的内容:

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

这是HTML代码;

  1. <div class="select-wrapper initialized">
  2. <span class="caret"></span>
  3. <input type="text" class="select-dropdown" readonly="true" data-activates="select-options-45625784-059c-3523-a00e-3ad824fad64a" value="-- Select --">
  4. <ul id="select-options-45625784-059c-3523-a00e-3ad824fad64a" class="dropdown-content select-dropdown ">
  5. <li class=""><span>-- Select --</span></li>
  6. <li class=""><span>Afghanistan</span></li>
  7. <li class=""><span>Canada</span></li>
  8. </ul>
  9. <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">
  10. <option label="-- Select --" value="string:" selected="selected">-- Select --</option>
  11. <option label="Afghanistan" value="string:AF">Afghanistan</option>
  12. <option label="Canada" value="string:AF">Canada</option>
  13. </select>
  14. </div>

这是我的selectCountry函数。

  1. By countryInput = By.xpath("//[@id=\"countryCode_inputfileddiv\"]/div/input");
  2. public void selectCountry() {
  3. WebElement dropdown = driver.findElement(countryInput);
  4. dropdown.click();
  5. List<WebElement> options = driver.findElements(By.id("select-options-b9a0d236-4008-830c-5319-9405f74b872f"));
  6. for (WebElement option : options) {
  7. String valueToSelect = option.getText().trim();
  8. System.out.println(valueToSelect);
  9. if (valueToSelect.equals("Canada")) {
  10. continue;
  11. }
  12. option.click();
  13. }
  14. }

如何点击下拉框?

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

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

英文:

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;

  1. &lt;div class=&quot;select-wrapper initialized&quot;&gt;
  2. &lt;span class=&quot;caret&quot;&gt;▼&lt;/span&gt;
  3. &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;
  4. &lt;ul id=&quot;select-options-45625784-059c-3523-a00e-3ad824fad64a&quot; class=&quot;dropdown-content select-dropdown &quot;&gt;
  5. &lt;li class=&quot;&quot;&gt;&lt;span&gt;-- Select --&lt;/span&gt;
  6. &lt;/li&gt;&lt;li class=&quot;&quot;&gt;&lt;span&gt;Afghanistan&lt;/span&gt;&lt;/li&gt;
  7. &lt;/li&gt;&lt;li class=&quot;&quot;&gt;&lt;span&gt;Canada&lt;/span&gt;&lt;/li&gt;
  8. &lt;/ul&gt;
  9. &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;
  10. &lt;option label=&quot;-- Select --&quot; value=&quot;string:&quot; selected=&quot;selected&quot;&gt;-- Select --&lt;/option&gt;
  11. &lt;option label=&quot;Afghanistan&quot; value=&quot;string:AF&quot;&gt;Afghanistan&lt;/option&gt;
  12. &lt;option label=&quot;Canada&quot; value=&quot;string:AF&quot;&gt;Canada&lt;/option&gt;
  13. &lt;/select&gt;
  14. &lt;/div&gt;

Here is my selectCountry function.

  1. By countryInput = By.xpath(&quot;//*[@id=\&quot;countryCode_inputfileddiv\&quot;]/div/input&quot;);
  2. public void selectCountry() {
  3. WebElement dropdown = driver.findElement(countryInput);
  4. dropdown.click();
  5. List&lt;WebElement&gt; options = driver.findElements(By.id(&quot;select-options-b9a0d236-4008-830c-5319-9405f74b872f&quot;));
  6. for (WebElement option : options) {
  7. String valueToSelect = option.getText().trim();
  8. System.out.println(valueToSelect);
  9. if (valueToSelect.equals(&quot;Canada&quot;)) {
  10. continue;
  11. }
  12. option.click();
  13. }
  14. }

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类来处理/选择下拉框的值。参考下面的代码:

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

所需的导入:

  1. 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:

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

Imports required:

  1. 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

英文:
  1. public void selectCountry() {
  2. WebElement dropdown = driver.findElement(By.id(&quot;countryCode&quot;));
  3. Select select=new Select(dropdown);
  4. select.selectByVisibleText(&quot;Afghanistan&quot;);
  5. }
  6. 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:

确定