Selenium Web Driver中选择器的问题

问题描述 投票:4回答:3

我有一些选择器在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();

感谢您的一切建议!

java selenium selenium-webdriver drop-down-menu webdriver
3个回答
0
投票

根据你共享的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");

0
投票

此下拉列表使用select和option标签,因此您可以使用selenium中的select class。

Select drop_down = new Select(driver.findElement(By.id("demo-htmlselect")));
drop_down.selectByVisibleText("Menthol");

要么

drop_down.selectByValue("432");

0
投票

这不是从选择框中选择选项的正确方法。

Select

你必须使用这样的东西:

WebElement element = <WEB DRIVER INSTANCE>.findElement(By.xpath(SELECTOR FOR THE SELECT ELEMENT));
Select select = new Select(element);
select.selectByValue(<VALUE OF THE SELECTED ITEM>);
© www.soinside.com 2019 - 2024. All rights reserved.