【发布时间】:2017-06-15 05:34:43
【问题描述】:
我正在尝试从 Selenium Webdriver 的下拉列表中选择一个值。但是每次我在控制台中出现错误时
这是我的网站URL,下面是元素的快照:
我试过下面的代码,但它不起作用
Select dropdown= new Select(driver.findElement(By.xpath("//div[@on-dimension-select = 'selectQuantityDimension']/*[@role='combobox']")));
dropdown.selectByValue("4");
得到以下异常:-
线程“main”中的异常 org.openqa.selenium.support.ui.UnexpectedTagNameException:元素 应该是“选择”但是“跨度”构建信息:版本: '未知',修订:'1969d75',时间:'2016-10-18 09:43:45 -0700'
也尝试过这种方式:
driver.findElement(By.xpath("//div[@on-dimension-select = 'selectQuantityDimension']/*[@role='combobox']")).click();
Select dropdown= new Select(driver.findElement(By.xpath("//div[@on-dimension-select = 'selectQuantityDimension']/select/")));
dropdown.selectByValue("4");
而这次的错误是:
线程“main”中的异常 org.openqa.selenium.InvalidSelectorException:无效选择器:无法 使用 xpath 表达式定位元素 //div[@on-dimension-select = 'selectQuantityDimension']/select/ 因为以下错误: SyntaxError: Failed to execute '文档'上的'评估':字符串'//div [@on-dimension-select = 'selectQuantityDimension']/select/' 不是有效的 XPath 表达式。
这是相同的 HTML 代码:
<div class="ng-isolate-scope ng-pristine ng-valid" ng-show="!docked" ng-model="variantOptionsModel" on-dimension-select="selectQuantityDimension" docked="docked">
<label>Quantity:</label>
<select class="accessory-option ng-pristine ng-valid" ng-model="model.selectedQuantity" ng-options="quantity for quantity in model.quantityDropDownValues track by quantity" ng-change="quantityClick()" data-qa-quantity-value="" style="display: none;">
<option value="1" selected="selected" label="1">1</option>
<option value="2" label="2">2</option>
<option value="3" label="3">3</option>
<option value="4" label="4">4</option>
<option value="5" label="5">5</option>
<option value="6" label="6">6</option>
</select>
<span id="" class="selectboxit-container selectboxit-container" role="combobox" aria-autocomplete="list" aria-haspopup="true" aria-expanded="false" aria-owns="">
<span id="" class="selectboxit accessory-option ng-pristine ng-valid selectboxit-enabled selectboxit-btn" name="" tabindex="0" unselectable="on" data-qa-quantity-value="">
<ul class="selectboxit-options selectboxit-list" tabindex="-1" role="listbox" aria-hidden="true" style="max-height: 217px; top: -217px; display: none;">
</span>
</div>
我不确定自己做错了什么。
【问题讨论】:
-
在第二个示例中删除
select之后的/。 -
@DevTester85 您的评论
trying to automate the dropdown我们不清楚。您可以考虑更新有关您尝试执行的确切步骤的问题吗?谢谢
标签: java selenium selenium-webdriver