无法从Selenium Webdriver的下拉列表中选择隐藏元素

时间:2019-02-13 00:18:34

标签: javascript java selenium-webdriver

HTML代码:

<select class="select2 ddl visible select2-hidden-accessible" data-allow-clear="true" id="Step1Model_CampaignAdditionalDataTypeId" multiple="" name="Step1Model.CampaignAdditionalDataTypeId" tabindex="-1" aria-hidden="true">
<option value="1">Optus AC Number</option>
<option value="2">Date Of Birth</option>
<option value="3">Pass Phrase</option>
<option value="4">Transaction Id</option>
</select>
<span class="select2 select2-container select2-container--default select2-container--above" dir="ltr" style="width: 121.047px;">
<span class="selection">
<span class="select2-selection select2-selection--multiple" role="combobox" aria-haspopup="true" aria-expanded="false" tabindex="-1">
<ul class="select2-selection__rendered">
<li class="select2-search select2-search--inline">
<input class="select2-search__field" type="search" tabindex="0" autocomplete="off" autocorrect="off" autocapitalize="none" spellcheck="false" role="textbox" aria-autocomplete="list" placeholder="" style="width: 0.75em;"></li>
</ul>
</span></span>
<span class="dropdown-wrapper" aria-hidden="true">
</span></span>

我正在Selenium Webdriver(带有Java)中使用以下代码(POM)进行选择:

public static By campAddDataType= By.id("Step1Model_CampaignAdditionalDataTypeId");
waitTime = new WebDriverWait(driver,20);
waitTime.until(ExpectedConditions.visibilityOf(driver.findElement(CommonOR.campAddDataType)));
String js = "arguments[0].hidden='false'"; 

((JavascriptExecutor) driver).executeScript(js,(driver.findElement(CommonOR.campAddDataType)));

CampAdd = new Select(driver.findElement(CommonOR.campAddDataType));
CampAdd.selectByVisibleText("Date Of Birth");

当用户将一个选项标记为true时,将显示此下拉列表。所以我相信这就是为什么-> aria-hidden =“ true” 为了使此选项为false,我运行了Java脚本执行程序,但仍然没有从下拉列表中选择所需的元素。

请提出一些解决方案。

1 个答案:

答案 0 :(得分:0)

您尝试过以下内容吗?

listItem = driver.findElement(By.xpath("//select[@id='Step1Model_CampaignAdditionalDataTypeId']/option[.='Date Of Birth']"))
((JavascriptExecutor) driver).executeScript("arguments[0].click();",listItem);