java - Selenium Web 驱动程序中的选择器问题

标签 java selenium selenium-webdriver drop-down-menu webdriver

我在使用 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 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 27 mg/ml &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;                     </option>
         <option value="432" data-imagesrc="https://logicvapes-us-dev.jtiweb.co.uk/media/catalog/swatches/4/45x45/media/menthol.png"
                                            data-description=""> Menthol &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 27 mg/ml &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;                     </option>
         <option value="435" data-imagesrc="https://logicvapes-us-dev.jtiweb.co.uk/media/catalog/swatches/4/45x45/media/cherry.png"
                                            data-description=""> Cherry &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 27 mg/ml &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;                     </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类 an 额外诱导 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");

关于java - Selenium Web 驱动程序中的选择器问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52383022/

相关文章:

java - 如何使用 poi 设置数据透视表中行标签的默认值

java - Spring-Websocket:满足条件时向订阅者发送更新

java - DecimalFormatSymbols.getInstance().getDecimalSeparator() 将输出返回为 ',' 44 而不是 ','

java - Selenium - 错误 : The expression is not a legal expression for xpath

javascript - 无法使用带有 javascript 和 jest 框架的 selenium 来关闭自动化中的 cdk 下拉列表

selenium - 使用 Selenium WebDriver 从 PortableApps 运行 Chrome 失败

python-3.x - Selenium Python 不会关闭子窗口

java - 如何在android中每1分钟移动一次获得不同的纬度和经度?

java - 我无法使用 Selenium WebBrowser Java 打开任何内容

c# - 在 Selenium 中检查页面加载时间