我有以下代码:
<span class="dropDown">Choose</span>
<div class="dropdownQuestions active" style="display: block; max-height: 310px;">
<span data-index="0" data-value="1" onclick="gt.setQuestion(this, true);">Warrior</span>
<span data-index="1" data-value="2" onclick="gt.setQuestion(this, true);">Mage</span>
<span data-index="2" data-value="3" onclick="gt.setQuestion(this, true);">Sorcerer</span>
如果可能的话,我想要的是一个下拉列表的列表,这样在我可以使用其中一个来单击之后,我进行了很多搜索,但找不到任何对我有帮助的答案。
其他选项是使用数据索引或数据值字符串进行单击。 我想要的最后一个选项是单击字符串“战士”、“法师”或“巫师”,但如果没有其他方法,我会很高兴。
谢谢!
最佳答案
点击下拉菜单后,您可以获取列表中的所有下拉选项,然后根据需要使用它们(循环并选择具有特定条件的所需选项或通过索引获取选项)。
List<WebElement> options= driver.findElements(By.xpath("//span[@class='dropDown']/div/span"));
options.get(1).click(); // here 1 is the index value of the option to select or you can use loop if looking for some specific condition
对于您在评论中分享的图像链接,请尝试以下 cssSelector:
List<WebElement> option= driver.findElements(By.cssSelector("div.questionDropdownOptions.activeSelectMenu span"));
option.get(1).click();
}
注意:请在您的代码中导入以下包:
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
关于java - Selenium - 如何使用 java 获取下拉列表的选项列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61312533/