Java 如何使用selenium选择自定义下拉列表元素
我有一个项目,其中隐藏了本机下拉列表,并使用了自定义下拉列表。因此,我无法使用selenium select方法,并且select选项被隐藏且不可用。所有下拉列表“称呼”、“国家”、“比特日期”等均作为自定义下拉列表实现Java 如何使用selenium选择自定义下拉列表元素,java,selenium,drop-down-menu,selenium-webdriver,webdriver,Java,Selenium,Drop Down Menu,Selenium Webdriver,Webdriver,我有一个项目,其中隐藏了本机下拉列表,并使用了自定义下拉列表。因此,我无法使用selenium select方法,并且select选项被隐藏且不可用。所有下拉列表“称呼”、“国家”、“比特日期”等均作为自定义下拉列表实现 <div class="medium-8 large-4 left column"> <select name="register[personal][salutation]" id="register_personal_salutation" styl
<div class="medium-8 large-4 left column">
<select name="register[personal][salutation]" id="register_personal_salutation" style="display: none;">
<option value="mr">Herr</option>
<option value="ms">Frau</option>
</select>
<div class="select-dropdown">
<div class="selected">Herr</div>
<div class="choices">
<ul>
<li>Herr</li>
<li>Frau</li>
</ul>
</div>
</div>
</div>
解决方案1不太好:单击选择下拉列表并单击选项。它可以工作,但不能重复使用
首选解决方案2:实现自己的可重用customSelect方法,这样我就可以通过给定的选择器和选项字符串(如webdriver select)选择正确的选项。但是我不知道如何实现这样的功能。是否有人已经使用过自定义下拉列表并提供了解决方案或提示?好的,这是我的解决方案。optionClass是选项的css选择器,option是应选择的值。我认为这不是最好的解决办法,而是解决问题的一种方法。有任何反馈、改进或想法吗
public void customSelect(String optionClass, String option) {
log.trace("I custom select the option " + option + " from the dropdown");
int amountOptions = this.getNumOfElements(By.cssSelector(optionClass));
for (int pos = 1; pos <= amountOptions; pos++) {
String sortOption = this.getText(By.cssSelector(optionClass + ":nth-child(" + pos + ")"));
if (sortOption.equalsIgnoreCase(option)) {
this.click(By.cssSelector(optionClass + ":nth-child(" + pos + ")"));
return;
}
}
throw new NoSuchElementException("The dropdown does not contain the option " + option
+ " and therefore it is not possible to select it.");
}
好的,这是我的解决方案。optionClass是选项的css选择器,option是应选择的值。我认为这不是最好的解决办法,而是解决问题的一种方法。有任何反馈、改进或想法吗
public void customSelect(String optionClass, String option) {
log.trace("I custom select the option " + option + " from the dropdown");
int amountOptions = this.getNumOfElements(By.cssSelector(optionClass));
for (int pos = 1; pos <= amountOptions; pos++) {
String sortOption = this.getText(By.cssSelector(optionClass + ":nth-child(" + pos + ")"));
if (sortOption.equalsIgnoreCase(option)) {
this.click(By.cssSelector(optionClass + ":nth-child(" + pos + ")"));
return;
}
}
throw new NoSuchElementException("The dropdown does not contain the option " + option
+ " and therefore it is not possible to select it.");
}
对于我来说,下面的方法在下拉列表中也有默认值的情况下工作得很好。 假设大多数下拉列表的默认值为“未设置”,您可以尝试以下方法:
internal static void DropDownByValue(string defaultValue, string chosenValue)
{
DriverInstance.Wait(TimeSpan.FromSeconds(1.5));
var valueName =
DriverInstance.Driver.FindElement(By.XPath("//option[contains (text (), '" + defaultValue + "')]"));
var parent = valueName.FindElement(By.XPath(".."));
SelectFromDropdown(parent, chosenValue);
}
public static void SelectFromDropdown(IWebElement button, string value)
{
var dropdown = new SelectElement(button);
dropdown.SelectByText(value);
希望这能有所帮助。对于我来说,如果下拉列表也有一个默认值,下面的方法就可以了。 假设大多数下拉列表的默认值为“未设置”,您可以尝试以下方法:
internal static void DropDownByValue(string defaultValue, string chosenValue)
{
DriverInstance.Wait(TimeSpan.FromSeconds(1.5));
var valueName =
DriverInstance.Driver.FindElement(By.XPath("//option[contains (text (), '" + defaultValue + "')]"));
var parent = valueName.FindElement(By.XPath(".."));
SelectFromDropdown(parent, chosenValue);
}
public static void SelectFromDropdown(IWebElement button, string value)
{
var dropdown = new SelectElement(button);
dropdown.SelectByText(value);
希望这能有所帮助。当你发布HTML时,请花一分钟时间使用一个美化工具,如正确格式化。它使阅读更容易,使你的问题更有可能得到回答。谢谢你尝试过什么,结果如何?就像你在学校做的那样。。。请出示你的作品这是获得问题答案的过程的一部分。它对你很有帮助,因为它迫使你调查自己的问题并仔细思考。它还向读者证明,你做了功课,并做出了合理的尝试来回答自己的问题。第三,它帮助读者发现并诊断问题,从而为您提供更好的答案,减少我们浪费的时间。当您发布HTML时,请花一分钟时间使用美化工具,如正确设置格式。它使阅读更容易,使你的问题更有可能得到回答。谢谢你尝试过什么,结果如何?就像你在学校做的那样。。。请出示你的作品这是获得问题答案的过程的一部分。它对你很有帮助,因为它迫使你调查自己的问题并仔细思考。它还向读者证明,你做了功课,并做出了合理的尝试来回答自己的问题。第三,它帮助读者发现并诊断问题,从而为您提供更好的答案,减少我们浪费的时间。