自动填充selenium webdriver

时间:2016-11-25 11:31:53

标签: selenium autocomplete webdriver dropdown

我需要从selenium webdriver中的auto complete下拉列表中选择地址。

我已经添加了下拉框的inspect元素源,如下所示,

<head>
<body>
<input id="gridLogId" name="gridLogId" value="qOZcnJEE/G9A+fAN/C4PlA==" type="hidden"/>
<div id="wrapper">
<div id="confirmation-modal" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<script id="subscriptiondetailTemplate" type="text/x-kendo-template">   # for (var i = 0; i < data.length; i++) { # <span class="kendoTooltip cursorpointer subs_IAM fa subs_#= data[i].PlanCode#" title="Plan description : #= data[i].PlanDesc #<br>Expiry on : #= data[i].ExpiryOnLabel #">#= data[i].PlanDesc #</span> # } # </script>
<script>   var subscriptionTemplate = kendo.template($('#subscriptiondetailTemplate').html()); </script>
<style>   body { overflow: hidden !important; } </style>
<ul id="ui-id-1" class="ui-autocomplete ui-front ui-menu ui-widget ui-widget-content ui-corner-all" tabindex="0" style="display: none;"/>
<div class="k-list-container k-popup k-group k-reset" data-role="popup" style="display: none; position: absolute;">
<div id="Finyear_OWtyUEpDYnYyMFU9-list" class="k-list-container k-popup k-group k-reset" data-role="popup" style="display: none; position: absolute;">
<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: 444px; top: 742.85px; left: 460.133px;">
<li class="ui-menu-item" role="presentation">
<a id="ui-id-303" class="ui-corner-all" tabindex="-1">SYDNEY, New South Wales, Australia, 1001</a>
</li>
<li class="ui-menu-item" role="presentation">
<a id="ui-id-304" class="ui-corner-all" tabindex="-1">SYDNEY, New South Wales, Australia, 1002</a>
</li>
<li class="ui-menu-item" role="presentation">
<a id="ui-id-305" class="ui-corner-all" tabindex="-1">SYDNEY, New South Wales, Australia, 1003</a>
</li>
<li class="ui-menu-item" role="presentation">
<a id="ui-id-306" class="ui-corner-all" tabindex="-1">SYDNEY, New South Wales, Australia, 1005</a>
</li>
<li class="ui-menu-item" role="presentation">
<li class="ui-menu-item" role="presentation">
<li class="ui-menu-item" role="presentation">
<li class="ui-menu-item" role="presentation">
<li class="ui-menu-item" role="presentation">

我需要从自动填充框中选择“悉尼,新南威尔士,澳大利亚,1001”下拉值。请建议我一些想法。我使用了以下代码,但它不起作用,

driver.findElement(By.id("ResidentialOrBusinessAddress_City")).sendKeys("Sydney");
        Thread.sleep(1000);
        java.util.List<WebElement> autoSuggest = driver.findElements(By.partialLinkText("1001"));
        driver.findElement(By.id("ResidentialOrBusinessAddress_Region")).sendKeys("NSW");
        Thread.sleep(1000);
        java.util.List<WebElement> autoSuggest1 = driver.findElements(By.partialLinkText("1001"));
        driver.findElement(By.id("ResidentialOrBusinessAddress_PostCode")).sendKeys("1001");
        Thread.sleep(1000);
        java.util.List<WebElement> autoSuggest2 = driver.findElements(By.partialLinkText("1001"));
        driver.findElement(By.id("ResidentialOrBusinessAddress_Country")).sendKeys("Australia");
        Thread.sleep(1000);
        java.util.List<WebElement> autoSuggest3 = driver.findElements(By.partialLinkText("Australia"));

请建议我如何获得这些价值观。我已经附上了页面的屏幕截图。enter image description here

谢谢。

1 个答案:

答案 0 :(得分:2)

请尝试以下操作,它对我有用

WebElement select = driver.findElement(By.id(“ui-id-2”));

   List<WebElement> options = select.findElements(By.tagName("li"));

   for (WebElement option1 : options) {

   if("SYDNEY, New South Wales, Australia, 1001".equals(option1.getText().trim()))

    option1.click();   
   }