问题描述
我正在用 java 创建一个 selenium 测试。我想在对话中自动化下拉菜单。 下拉菜单的xpath为:
/html/body/div[8]/div/div/form/div[2]/div[2]/div[2]/div/select
new webdriverwait(driver,20).until
和 ExpectedCondition 来选择一个元素。你能帮我找到一种从下拉菜单中选择元素的方法吗。
解决方法
您需要先点击下拉按钮,然后从下拉列表中找到您要选择的按钮,然后点击它。
,要从 select-options 标签中选择 html-select 之一,您需要为 elementToBeClickable()
引入 WebDriverWait,您可以使用以下任一 Locator Strategies :
-
使用 id 和
selectByIndex()
:new Select(new WebDriverWait(driver,20).until(ExpectedConditions.elementToBeClickable(By.id("selectID")))).selectByIndex(1);
-
使用 cssSelector 和
selectByVisibleText()
:new Select(new WebDriverWait(driver,20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("selectCssSelector")))).selectByVisibleText("OptionText");
-
使用 xpath 和
selectByValue()
:new Select(new WebDriverWait(driver,20).until(ExpectedConditions.elementToBeClickable(By.xpath("selectXpath")))).selectByValue("OptionValue");
参考文献
您可以在以下位置找到一些相关的详细讨论: