简体   繁体   中英

Problem with selectors in Selenium Web Driver

I have a problem with some selectors to click in my automatic test in Selenium. My test doesn't see any of the selectors that I used. There is my div which I working with:

 <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> 

And my ideas (which aren't working):

 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(); 

Thanks for every advice!

As per the html you have shared the the element is a <select> tag so you have to use the Select class an additionally induce WebDriverWait to choose an option and you can use the following solution:

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");

This is not the correct way to select an option from a select box.

See Select

You have to use something like this:

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>);

This drop down is made using select and option tags, so you can use select class from selenium.

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

Or

drop_down.selectByValue("432");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM