使用selenium webdriver在自定义下拉列表中选择一个选项
我正在尝试使用SeleniumJava从下拉列表中选择一个选项。我也尝试过许多解决办法。在FirePath中执行XPath时找到country元素,但在运行脚本时找不到 硒代码:使用selenium webdriver在自定义下拉列表中选择一个选项,selenium,selenium-webdriver,Selenium,Selenium Webdriver,我正在尝试使用SeleniumJava从下拉列表中选择一个选项。我也尝试过许多解决办法。在FirePath中执行XPath时找到country元素,但在运行脚本时找不到 硒代码: driver.findElement(By.xpath("//*[@class='selectize-input items not-full has-options']")).click(); Thread.sleep(500); String countryXpath = "//*[@cl
driver.findElement(By.xpath("//*[@class='selectize-input items not-full has-options']")).click();
Thread.sleep(500);
String countryXpath = "//*[@class='select-item']/following::span[text()='India']";
System.out.println(countryXpath);
WebElement countryName = driver.findElement(By.xpath(countryXpath));
ScrollHelper.ScrollHorizontalUpToVisibilityOfElement(countryName);
countryName.click();
HTML代码:
<select class="selectized isfocused parsley-error" name="Country" data-input-parsley="" data-parsley-trigger="change input" data-parsley-required-message="This field should not empty" data-parsley-required="true" data-parsley-group="country-select" tabindex="-1" style="display: none;" data-parsley-id="17">
<option value="" selected="selected"/>
</select>
<div class="selectize-control single">
<div class="selectize-input items has-options not-full focus input-active dropdown-active">
<input autocomplete="off" tabindex="" style="width: 100%; opacity: 1; position: relative; left: 0px;" placeholder="" type="text"/>
</div>
<div class="selectize-dropdown single" style="display: block; visibility: visible; width: 413px; top: 32px; left: 0px;">
<div class="selectize-dropdown-content">
<div class="select-item" data-selectable="" data-value="6">
<div class="select-option">
<span class="text"> Australia </span>
</div>
</div>
<div class="select-item" data-selectable="" data-value="2">
<div class="select-option">
<span class="text"> Canada </span>
</div>
</div>
<div class="select-item" data-selectable="" data-value="21">
<div class="select-option">
<span class="text"> China </span>
</div>
</div>
<div class="select-item" data-selectable="" data-value="22">
<div class="select-option">
<span class="text"> India </span>
</div>
</div>
<div class="select-item" data-selectable="" data-value="25">
<div class="select-option">
<span class="text"> USA </span>
</div>
</div>
</div>
澳大利亚
加拿大
中国
印度
美国
尝试使用此xpath单击元素:
"//span[contains(text(), 'India')]"
让我知道它是否有效。在Html中,span text在country name之前有空格
<span class="text"> India </span>
上面提到的xpath将检查span的文本是否包含印度如果您能够单击居住国,那么您必须向下滚动一点点,然后此代码可能适用于您:
List<WebElement> countries = driver.findElements(By.cssSelector("div[class='select-option']>span"));
for(WebElement country : countries){
if(country.getText().trim().equals("Singapore"))
country.click();
}
List countries=driver.findElements(By.cssSelector(“div[class='select-option']>span”);
for(WebElement国家/地区:国家/地区){
if(country.getText().trim().equals(“Singapore”))
country.click();
}
如果您面临任何问题,请向下滚动,然后让我知道。您想选择哪个国家?您能用
outerHTML
的更多内容更新问题,以包含带有居住国文本的元素吗?实际上,我想选择列表末尾的国家名为“Singapore”。
List<WebElement> countries = driver.findElements(By.cssSelector("div[class='select-option']>span"));
for(WebElement country : countries){
if(country.getText().trim().equals("Singapore"))
country.click();
}