我正在使用注册页面,我有基本字段,如Salutation,名字,姓氏和地址详情。
Salutation下拉结构如下,
<div class="k-list-scroller" unselectable="on" style="height: 200px;">
<ul id="Salutation_listbox" class="k-list k-reset" unselectable="on" tabindex="-1" aria-hidden="false" aria-live="off" data-role="staticlist" role="listbox">
<li id="0741907a-4369-4ac2-8100-3829655a7522" class="k-item k-state-selected k-state-focused" data-offset-index="0" unselectable="on" role="option" tabindex="-1">Salutation</li>
<li class="k-item" data-offset-index="1" unselectable="on" role="option" tabindex="-1" style="">Mr</li>
<li class="k-item" data-offset-index="2" unselectable="on" role="option" tabindex="-1">Mrs</li>
<li class="k-item" data-offset-index="3" unselectable="on" role="option" tabindex="-1">Miss</li>
<li class="k-item" data-offset-index="4" unselectable="on" role="option" tabindex="-1">Ms</li>
<li class="k-item" data-offset-index="5" unselectable="on" role="option" tabindex="-1">Dr</li>
<li class="k-item" data-offset-index="6" unselectable="on" role="option" tabindex="-1">Prof</li>
<li class="k-item" data-offset-index="7" unselectable="on" role="option" tabindex="-1">Rev</li>
</ul>
为了从下拉值中选择Mr我使用了以下代码,
String capColorDropDown = "//span[@role='listbox']";
driver.findElement(By.xpath(capColorDropDown)).click();
String itemName = "Orange";
String listId = "color_listbox";
Thread.sleep(2000);
String xpathForItem = "//ul[@id='Salutation_listbox']/li[@class='k-item' and text()='Mr']";
driver.findElement(By.xpath(xpathForItem)).click();
这段代码对我来说很好,我可以选择Mr值作为称呼,但现在我必须完成地址详细信息字段,其中city字段是自动完成下拉列表,其结构如下,< / p>
<div class="k-list-scroller" unselectable="on" style="height: auto;">
<ul id="AddressDetails_City_listbox" class="k-list k-reset" unselectable="on" tabindex="-1" aria-hidden="false" aria-live="polite" data-role="staticlist" role="listbox">
<li class="k-item" data-offset-index="0" unselectable="on" role="option" tabindex="-1" style="">ALPURRURULAM, NT, Australia, 4825</li>
<li class="k-item" data-offset-index="1" unselectable="on" role="option" tabindex="-1">ALPURRURULAM, null, Invalid, </li>
<li class="k-item" data-offset-index="2" unselectable="on" role="option" tabindex="-1">ALPURRURULAM, null, Invalid, </li>
</ul>
我正在尝试使用相同的代码访问ALPURRURULAM,NT,澳大利亚,4825值,
String capColorDropDown1 = "//span[@role='listbox']";
driver.findElement(By.xpath(capColorDropDown1)).click();
String itemName1 = "Orange";
String listId1 = "color_listbox";
Thread.sleep(2000);
String xpathForItem1 = "//ul[@AddressDetails_City_listbox']/li[@class='k-item' and text()='ALPURRURULAM, NT, Australia, 4825']";
driver.findElement(By.xpath(xpathForItem)).click();
但是selenium正在访问//span[@role='listbox]
并再次点击称呼下拉列表。城市字段也与listbox
具有相同的作用,但如何区分和选择城市字段值?
答案 0 :(得分:0)
您应该尝试使用更具体的定位器,如下所示: -
WebElement el = driver.findElement(By.id("AddressDetails_City_listbox"));
el.click();
//now select desired option
new WebDriverWait(driver, 10).until(ExpectedConditions.presenceOfNestedElementLocatedBy(el, By.cssSelector("li[data-offset-index='0']"))).click();