java - Selenium Web 驱动程序中的选择器问题
问题描述
我在 Selenium 中的自动测试中单击某些选择器时遇到问题。我的测试没有看到我使用的任何选择器。我正在使用我的 div:
<select name="people" id="demo-htmlselect" onchange="selectConfigOption(this.value)" >
<option value="">Choose a selection...</option>
<option value="429" data-imagesrc="https://logicvapes-us-dev.jtiweb.co.uk/media/catalog/swatches/4/45x45/media/tobacco.png"
data-description=""> Tobacco 27 mg/ml </option>
<option value="432" data-imagesrc="https://logicvapes-us-dev.jtiweb.co.uk/media/catalog/swatches/4/45x45/media/menthol.png"
data-description=""> Menthol 27 mg/ml </option>
<option value="435" data-imagesrc="https://logicvapes-us-dev.jtiweb.co.uk/media/catalog/swatches/4/45x45/media/cherry.png"
data-description=""> Cherry 27 mg/ml </option>
</select>
我的想法(不起作用):
wd = new FirefoxDriver();
WebElement span = wd.executeScript("return document.getElementById('dd-select');");
wd.findElement(span).click();
//wd.findElement(By.xpath("//div[@class='dd-select']/span[@class='class='dd-pointer.dd-pointer-down'']")).click();
//wd.findElement(By.xpath("value=//*[@id='432']"));
//WebElement register = wd.findElement(By.name('people'));
//wd.findElement(By.partialLinkText("Choose a selection...")).click();
//wd.findElementById("select=//*[@id='429']").click();
感谢您的每一个建议!
解决方案
根据您共享的 html,该元素是一个<select>
标签,因此您必须使用Select
该类并另外诱导WebDriverWait选择一个选项,您可以使用以下解决方案:
WebElement elem = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//select[@id='demo-htmlselect' and @name='people']")));
Select mySelect = new Select(elem);
//selecting the second item by value 429
mySelect.selectByValue("429");
推荐阅读
- julia - LoadError:Julia for 循环中的 UndefVarError
- java - Android WorkManager 和 Worker
- gcc - arm-none-eabi-gcc 与 nucleo L432KC 板
- java - 无法为简单的控制器运行 Spring Boot 测试
- python - 是否可以从条目小部件或文本小部件在树视图中插入多行文本?或类似的东西?
- c# - 自定义调整窗口大小
- python - Python Tkinter 应用程序将焦点从打开的文件夹中移开
- javascript - 如何条带化 HTML标记并将其替换为
? - macos - macOS (AppKit/SwiftUI) 中的自定义对象全选/取消选择菜单项
- c# - 如何在 .NET Core 中使用依赖注入注册多个项目?