1

<div class="Select form-control undefined Select--single is-searchable has-value"><input name="deal.listdealasset[0].acacode" type="hidden" value="NEWCAR"><div class="Select-control"><span class="Select-multi-value-wrapper" id="react-select-3--value"><div class="Select-value"><span class="Select-value-label" id="react-select-3--value-item" role="option" aria-selected="true">New</span></div><div class="Select-input" style="display: inline-block;"><style>input#undefined::-ms-clear {display: none;}</style><input role="combobox" aria-expanded="false" aria-haspopup="false" aria-activedescendant="react-select-3--value" aria-owns="" style="width: 19px; box-sizing: content-box;" value="" data-rxName="deal.listdealasset[0].acacode"><div style="left: 0px; top: 0px; height: 0px; text-transform: none; letter-spacing: normal; overflow: scroll; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-style: normal; font-weight: 400; white-space: pre; visibility: hidden; position: absolute;"></div></div></span><span class="Select-arrow-zone"><span class="Select-arrow"></span></span></div></div>

I'm trying to select a value from a drop-down but its html tag is not select. I have tried various options but none work.

WebElement optionsList = driver.findElement(By.xpath("//span[contains(@class, 'Select-multi-value-wrapper')]"));
List<WebElement> options = optionsList.findElements(By.xpath("//span[contains(@class, 'Select-value-label')]"));
options.get(0).click();

I want to select the value new from drop down.[enter image description here]

I'm getting below error on execution - Element could not be scrolled into view

Below is the HTML code for the page-

<div class="Select form-control undefined Select--single is-searchable has-value">
   <input name="deal.listdealasset[0].acacode" type="hidden" value="NEWCAR">
   <div class="Select-control">
      <span class="Select-multi-value-wrapper" id="react-select-3--value">
         <div class="Select-value"><span class="Select-value-label" id="react-select-3--value-item" role="option" aria-selected="true">New</span></div>
         <div class="Select-input" style="display: inline-block;">
            <style>input#undefined::-ms-clear {display: none;}</style>
            <input role="combobox" aria-expanded="false" aria-haspopup="false" aria-activedescendant="react-select-3--value" aria-owns="" style="width: 19px; box-sizing: content-box;" value="" data-rxName="deal.listdealasset[0].acacode">
            <div style="left: 0px; top: 0px; height: 0px; text-transform: none; letter-spacing: normal; overflow: scroll; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-style: normal; font-weight: 400; white-space: pre; visibility: hidden; position: absolute;"></div>
         </div>
      </span>
      <span class="Select-arrow-zone"><span class="Select-arrow"></span></span>
   </div>
</div>
5
  • Possible duplicate of Selecting from div class dropdown - Selenium Commented May 13, 2019 at 6:15
  • tried with div but did not work Commented May 13, 2019 at 6:17
  • 1
    Can you share HTML code ? taking everything in a list of web element, and then try to click on one of the web element is the standard way to do it. Commented May 13, 2019 at 6:19
  • I have shared image of html code Commented May 13, 2019 at 6:21
  • Can you share that in plain text, It'd be easy for us to copy the HTML code and give you proper solution. and how many options are present in drop down anyway ? Commented May 13, 2019 at 6:21

4 Answers 4

0

Since your drop down is made of Div and span tags, Select class will not work from selenium.

Code you can try :

List<WebElement> options = driver.findElements(by.xpath(" your locator"));
for(WebElement element : options){
 if(element.getText().equals(" your value from drop down")){
    element.click();
}
}  

Let me know, if you have any more concerns.

Sign up to request clarification or add additional context in comments.

7 Comments

WebElement optionsList = driver.findElement(By.xpath("//span[contains(@class, 'Select-multi-value-wrapper')]")); List<WebElement> options = optionsList.findElements(By.xpath("//span[contains(@class, 'Select-value-label')]")); options.get(0).click();
I have tried with xpath as well but the value is still not selected
shared the html tag kindly let me know a solution for same...there are 2 values in drop down - New and Used
WebElement optionsList = driver.findElement(By.xpath("//span[contains(@class, 'Select-multi-value-wrapper')]")); List<WebElement> options = optionsList.findElements(By.xpath("//span[contains(@class, 'Select-value-label')]")); for(WebElement option : options) { if (option.getText().equals("New")) { option.click(); } } ...on executing above no error is displayed nor the value is getting selected
First you have to write the code to click on drop down, then only you can use my solution.
|
0

Since You are getting error of element can not be scrolled into view. You should try JavascriptExecutor for scrolling.

  1. This way will allow you to scroll horizontally/vertically page ((JavaScriptExecutor)driver).executeScript("scroll(0,200)") - only scroll vertically by 200 pixels

2 . For bringing element into Focus

((JavaScriptExecutor)driver).executeScript("arguments[0].scrollIntoView(true);", element);

1 Comment

tried javascript - //JavascriptExecutor je = (JavascriptExecutor) driver; //WebElement element = driver.findElement(By.xpath("//span[contains(@class,'Select-value-label')]")); //je.executeScript("arguments[0].scrollIntoView(true);",element);....but getting same cannot be scrolle dinto view error
0

Since option 'new' had id attribute defined, clicking object is straight forward by using id. Below code should work, provided that dropdown button is already clicked.

driver.findElement(By.id("react-select-3--value")).click();
driver.findElement(By.id("react-select-3--value-item")).click();

5 Comments

This only clicks on the drop down , does not select the "New" value from list
Edit : I added code to click on new value in the list. Please try and let know.
I gives following error -Unable to locate element: #react\-select\-3\-\-value\-item
After lots of attempts - following code will type New in the drop down field - driver.findElement(By.xpath("/html/body/section/div/div[2]/section/div/from/div/div[3]/div[1]/div/div/div[2]/div/div/div/div/div/span[1]/div[2]/input")).sendKeys("New");
But still this is not allowing to use enter key as if we follow manual steps it normally would select "New"-driver.findElement(By.xpath("//span[(@id='react-select-10--value')]")).sendKeys(Keys.ENTER);
0

Thanks for the response . It worked with below query i.e by merging send keys and keys.enter-

driver.findElement(By.id("react-select-3--value")).click();
driver.findElement(By.xpath("/html/body/section/div/div[2]/section/div/from/div/div[3]/div[1]/div/div/div[2]/div/div/div/div/div/span[1]/div[2]/input")).sendKeys("New",Keys.ENTER);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.