下拉列表在selenium webdriver中无效

时间:2017-06-29 11:28:57

标签: java selenium testing selenium-webdriver automation

我正在从Excel文件向Dropdown文本框发送文本。因此,它应该将文本发送到下拉文本框,并选择可见的特定匹配文本。

这是HTML代码:

<div id="routingPanelLeft">
<p style="margin-bottom: 1em">Selecting an item will append to end of hierarchy</p>
<div id="srlSelectContainer">
<p>Select an SRL:</p>
<select id="srlSelect" class="routing-select" style="display: none;">
<option/>
<option value="11">AS-HTTS-US-CDN</option>
<option value="20">AS-HTTS-US-CORE</option>
<option value="19">AS-HTTS-US-HW</option>
<option value="15">AS-HTTS-US-LAN-SW</option>
<option value="8">AS-HTTS-US-NMS</option>
<option value="13">AS-HTTS-US-RP</option>
<option value="14">AS-HTTS-US-SEC</option>
<option value="12">AS-HTTS-US-VOICE</option>
<option value="16">AS-HTTS-US-Wireless</option>
<option value="7">AS-HTTS-WANSW</option>
<option value="22">AS-US-Unsupported-KW</option>
<option value="50">ATT-INDIA-KWL</option>
<option value="33">ATT-KWL</option>
<option value="63">BELL-IPCC-UC</option>
<option value="52">BWI-GMBH-KWL</option>
<option value="65">CISCO_IT_HTTS</option>
<option value="27">EM1-KWL</option>
<option value="29">EM2-KWL</option>
<option value="36">EMC_SOS</option>
<option value="6">HTTS</option>
<option value="55">HTTS-AMAZON</option>
<option value="57">HTTS-CNS-KWL</option>
<option value="35">HTTS-COMCAST</option>
<option value="350">HTTS-Charter</option>
<option value="25">HTTS-EUEM</option>
<option value="58">HTTS-HSBC</option>
<option value="61">HTTS-HSBC-NCMKW</option>
<option value="43">HTTS-IOX</option>
<option value="42">HTTS-IPCC</option>
<option value="47">HTTS-IPCC-VZW</option>
<option value="68">HTTS-IT</option>
<option value="269">HTTS-MICROSOFT-SP</option>
<option value="60">HTTS-MSGNS</option>
<option value="56">HTTS-Mobility</option>
<option value="24">HTTS-Optus</option>
<option value="54">HTTS-PlatinumPlus</option>
<option value="51">HTTS-RPLAN-RNT</option>
<option value="53">HTTS-RUSSIAN</option>
<option value="49">HTTS-SP</option>
<option value="44">HTTS-SV</option>
<option value="45">HTTS-SVTAC</option>
<option value="26">HTTS-TEST</option>
<option value="10">HTTS-UCC</option>
<option value="66">HTTS-VIDEO</option>
<option value="48">HTTS-VTACPCMM</option>
<option value="41">HTTS-WAN</option>
<option value="70">HTTS-WANSW</option>
<option value="289">HTTS-WIRELESS</option>
<option value="34">HTTS_DT</option>
<option value="369">Jabber Support</option>
<option value="290">LATAM-HTTS</option>
<option value="309">LATAM-HTTS-EMEAR</option>
<option value="2">Lan Switching</option>
<option value="59">MDS_WW-SAN</option>
<option value="3">MultiService Specialization Queues</option>
<option value="64">Nexus-Amzn-kwl</option>
<option value="30">SEG1-KWL</option>
<option value="4">Security</option>
<option value="249">TSA</option>
<option value="62">TWC-SP</option>
<option value="37">VZB_APAC</option>
<option value="38">VZB_EMEA</option>
<option value="39">WW-SOS</option>
<option value="40">WW-SOS-CIM_CR2</option>
<option value="32">WW-SOS-HTTS</option>
<option value="31">WW-SOS-TAC</option>
<option value="46">htts-nexus7k</option>
<option value="389">test_1234</option>
</select>
<span class="custom-combobox">
<span class="ui-helper-hidden-accessible" role="status" aria-live="polite">45 results are available, use up and down arrow keys to navigate.</span>
<input class="custom-combobox-input ui-widget ui-widget-content ui-state-default ui-corner-left ui-autocomplete-input" title="" autocomplete="off"/>
<a class="ui-button ui-widget ui-state-default ui-button-icon-only custom-combobox-toggle ui-corner-right" tabindex="-1" title="Show All Items" role="button" aria-disabled="false">
<span class="ui-button-icon-primary ui-icon ui-icon-triangle-1-s"/>
<span class="ui-button-text"/>
</a>
</span>
</div>

<ul id="ui-id-2" class="ui-autocomplete ui-front ui-menu ui-widget ui-widget-content ui-corner-all" tabindex="0" style="display: block; width: 282px; top: -227.3px; left: 268.8px;">
<li class="ui-menu-item" data-type="undefined" data-id="11" role="presentation">
<a id="ui-id-27599" class="ui-corner-all" tabindex="-1">AS-HTTS-US-CDN</a>
</li>
<li class="ui-menu-item" data-type="undefined" data-id="20" role="presentation">
<a id="ui-id-27600" class="ui-corner-all" tabindex="-1">AS-HTTS-US-CORE</a
</ul>

我尝试过Select,Keys,Action类,SendKeys方法。但它没有从下拉列表中选择值。它正在向文本框发送文本,该文本框显示与文本选项匹配的结果,从中应选择我从Excel文件发送的值。

这些是我的努力:

WebDriverWait wait11 = new WebDriverWait(driver,5);
wait11.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id='srlSelectContainer']/span/input"))).click();
driver.findElement(By.xpath("//*[@id='srlSelectContainer']/span/input")).sendKeys(testData);

/*WebDriverWait wait11 = new WebDriverWait(driver,5);
wait11.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id='srlSelectContainer']/span/input"))).sendKeys(testData);
WebDriverWait wait1= new WebDriverWait(driver,5);
wait1.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id='srlSelectContainer']/span/input"))).sendKeys(Keys.ARROW_DOWN,Keys.ENTER);
WebDriverWait wait2= new WebDriverWait(driver,5);
wait2.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id='srlSelectContainer']/span/input"))).clear();
*/

WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id='srlSelectContainer']/span/input"))).click();*/

WebDriverWait wait2= new WebDriverWait(driver,5);
wait2.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id='srlSelectContainer']/span/input"))).sendKeys(testData);
driver.findElement(By.xpath("//*[@id='srlSelectContainer']/span/input")).sendKeys(Keys.ARROW_DOWN,Keys.ARROW_DOWN,Keys.ARROW_DOWN,Keys.ARROW_DOWN,Keys.ARROW_DOWN,Keys.ARROW_DOWN,Keys.ARROW_DOWN,Keys.ARROW_DOWN,Keys.ARROW_DOWN,Keys.ARROW_DOWN,Keys.ARROW_DOWN,Keys.ARROW_DOWN,Keys.ENTER);

请以任何方式建议我,因为这是一个障碍。

使用Chrome浏览器和selenium 3.40。

3 个答案:

答案 0 :(得分:1)

Selenium拥有处理<select>代码下拉列表的课程,Select

WebElement dropDown = driver.findElement(By.id("srlSelect"));
Select select = new Select(dropDown);
select.selectByVisibleText(testData);

答案 1 :(得分:0)

您似乎正在尝试访问隐藏元素,因为您可以看到有一个名为“display:none”的属性应用于元素,因此要访问该元素,您必须先将属性从none更改为block。希望这会有所帮助。

WebDriver driver; // Assigned elsewhere
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("document.getElementById('srlSelect').style.display='block'");

答案 2 :(得分:0)

这有效

没有硬编码值(但是要等待下一步!!!!)

NaN

OR 使用硬编码值

driver.findElement(By.xpath(".//*[@id='accountSelectContainer']/span/a/span[1]")).click();
        List<WebElement> options = driver.findElements(By.xpath("//ul[@id='ui-id-1']/li"));
        for (WebElement option : options) {
          if(testData.equals(option.getText()))
            option.click();