3

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!

Ratmir Asanov
  • 6,237
  • 5
  • 26
  • 40
Weirdo_Mrln
  • 103
  • 1
  • 10

3 Answers3

0

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");
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • I used your solution I have an error with locate of selectors: 09-18-2018 11:28:29 AM - [ERROR] - Test Cases/New Test Case FAILED because (of) (Stack trace: org.openqa.selenium.NoSuchElementException: Unable to locate element: //select[@id='demo-htmlselect' and @name='people'] – Weirdo_Mrln Sep 18 '18 at 09:29
  • @Weirdo_Mrln Checkout my updated answer and let me know the status – undetected Selenium Sep 18 '18 at 09:34
  • Same problem as last time: Test Cases/New Test Case FAILED because (of) (Stack trace: org.openqa.selenium.TimeoutException: Expected condition failed: waiting for element to be clickable: By.xpath: //select[@id='demo-htmlselect' and @name='people'] (tried for 20 second(s) with 500 MILLISECONDS interval) – Weirdo_Mrln Sep 18 '18 at 09:49
  • @Weirdo_Mrln Checkout my updated answer and let me know the status. – undetected Selenium Sep 18 '18 at 09:56
  • I used your method, and still I have problem. (caused by: (Stack trace: org.openqa.selenium.NoSuchElementException: Cannot locate an element using By.xpath: //select[@id='demo-htmlselect' and @name='people'] – Weirdo_Mrln Sep 18 '18 at 10:31
  • I trying to use this method with other selectors (by text, by value) and I don't have idea what is wrong :( – Weirdo_Mrln Sep 18 '18 at 10:33
0

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>);
kamentk
  • 513
  • 3
  • 12
0

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");
cruisepandey
  • 28,520
  • 6
  • 20
  • 38
  • I had to change driver(). method to variable, because there was an error. Nevertheless, I still have an error regarding the incorrect location of the element. And below I add some logs: – Weirdo_Mrln Sep 18 '18 at 10:06
  • Capabilities {acceptInsecureCerts: true, browserName: firefox, browserVersion: 62.0, javascriptEnabled: true, moz:accessibilityChecks: false, moz:headless: false, moz:processID: 97723, moz:profile: /var/folders/y2/_xzf44bx5k1..., moz:useNonSpecCompliantPointerOrigin: false, moz:webdriverClick: true, pageLoadStrategy: normal, platform: MAC, platformName: MAC, platformVersion: 17.7.0, rotatable: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}} – Weirdo_Mrln Sep 18 '18 at 10:07
  • *** Element info: {Using=id, value=demo-htmlselect} Test Cases/New Test Case FAILED because (of) (Stack trace: org.openqa.selenium.NoSuchElementException: Unable to locate element: #demo\-htmlselect – Weirdo_Mrln Sep 18 '18 at 10:07