Java 如何在selenium web驱动程序中选择剑道下拉列表?

Java 如何在selenium web驱动程序中选择剑道下拉列表?,java,selenium,selenium-webdriver,Java,Selenium,Selenium Webdriver,我是selenium web驱动程序的初学者。我无法选择剑道下拉列表 这是我的密码: Select profcat = new Select (driver.findElement(By.xpath("/html/body/div[1]/div[2]/section[2]/section/div/div[2]/div/div/form/div/div/div[1]/div/div[1]/div[4]/div/div/div/span/span/span[1]"))); profcat.selec

我是selenium web驱动程序的初学者。我无法选择剑道下拉列表

这是我的密码:

Select profcat = new Select (driver.findElement(By.xpath("/html/body/div[1]/div[2]/section[2]/section/div/div[2]/div/div/form/div/div/div[1]/div/div[1]/div[4]/div/div/div/span/span/span[1]")));
profcat.selectByIndex(2);

剑道UI没有常见的下拉列表。因此,您的代码将无法使用它。您需要使用下一个算法:

单击按钮打开下拉列表。 等待元素的出现,它包含我在chrome中使用debug获取该元素的下拉列表中的值。 在出现的值列表中迭代,然后单击所需的值。
我使用这种算法来处理这种下拉列表。

剑道UI没有常见的下拉列表。因此,您的代码将无法使用它。您需要使用下一个算法:

单击按钮打开下拉列表。 等待元素的出现,它包含我在chrome中使用debug获取该元素的下拉列表中的值。 在出现的值列表中迭代,然后单击所需的值。
我使用这种算法来处理这种下拉列表。

要从下拉列表中选择一个项目,需要编写XPath来查询要选择的元素的文本。我还将在您希望单击的选项上调用WebDriverWait,以说明下拉选项的任何延迟加载时间:

// expand the dropdown -- click on the div, may need to click something else
driver.findElement(By.xpath("//div[ul[@id='ddlCategoryTy_listbox']]")).click()

// wait on an option to exist
WebDriverWait wait = new WebDriverWait(driver, 10);
optionToClick = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//li[text()='Health Club']")));

// click the option
optionToClick.click();
您需要导入org.openqa.selenium.support.ui.ExpectedConditions和org.openqa.selenium.support.ui.WebDriverWait才能使其工作


上面的代码单击div元素展开下拉列表,然后使用XPath选择选项“Health Club”,该选项查询li文本。您可能需要更新用于扩展下拉列表的选择器-根据您提供的HTML,我无法判断哪个元素将打开下拉列表以显示选项。

要从下拉列表中选择一个项目,您需要编写XPath来查询要选择的元素的文本。我还将在您希望单击的选项上调用WebDriverWait,以说明下拉选项的任何延迟加载时间:

// expand the dropdown -- click on the div, may need to click something else
driver.findElement(By.xpath("//div[ul[@id='ddlCategoryTy_listbox']]")).click()

// wait on an option to exist
WebDriverWait wait = new WebDriverWait(driver, 10);
optionToClick = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//li[text()='Health Club']")));

// click the option
optionToClick.click();
您需要导入org.openqa.selenium.support.ui.ExpectedConditions和org.openqa.selenium.support.ui.WebDriverWait才能使其工作


上面的代码单击div元素展开下拉列表,然后使用XPath选择选项“Health Club”,该选项查询li文本。您可能需要更新用于展开下拉列表的选择器-根据您提供的HTML,我无法确定哪个元素将打开下拉列表以显示选项。

使用相关HTML更新问题使用相关HTML更新问题HTML@MurugesanK很高兴看到它对你有用。请随意将此帖子标记为您问题的答案,以便其他用户看到它的帮助。@MurugesanK很高兴看到它对您有用。请随意将此帖子标记为您问题的答案,以便其他用户看到它的帮助。