0

I have a small doubt over here on selecting one of the value from the drop down using selenium . When i generated a code using selenium-IDE it gave me a set of codes for selecting from the drop-down . Code is as follows-->

driver.findElement(By.cssSelector("input.search")).click();
driver.findElement(By.xpath("//table[@id='project-add-table']/tbody/tr[4]/td[3]/div/div[2]/div[3]")).click();
new Select(driver.findElement(By.cssSelector("select"))).selectByVisibleText("Test");

So can anybody tell what is the use of that second line of code ?After clicking to the dropdown why cant we directly select the required element?

HTML -->

<td class="environmentTd" required="">
   <div class="ui fluid search dropdown env selection">
      <select>
         <option value="SB">Sandbox</option>
         <option value="DEV">Development</option>
         <option value="QA">Test</option>
         <option value="PROD">Production</option>
         <option value="PP">Pre Production</option>
         <option value="UAT">UAT</option>
         <option value="DR">DR</option>
      </select>
      <i class="dropdown icon"></i><input class="search" autocomplete="off" tabindex="0">
      <div class="text">Sandbox</div>
      <div class="menu transition hidden" tabindex="-1">
         <div class="item active selected" data-value="SB">Sandbox</div>
         <div class="item" data-value="DEV">Development</div>
         <div class="item" data-value="QA">Test</div>
         <div class="item" data-value="PROD">Production</div>
         <div class="item" data-value="PP">Pre Production</div>
         <div class="item" data-value="UAT">UAT</div>
         <div class="item" data-value="DR">DR</div>
      </div>
   </div>
</td>

11
  • only third line of code is enough to select the value from dropdown. may be, first two lines are not related to dropdown selection. Commented Jan 17, 2018 at 5:05
  • Not it wont work with 3rd line alone .It gives exception .Its not able to find the element Commented Jan 17, 2018 at 5:12
  • is it working without second line of code? Commented Jan 17, 2018 at 5:26
  • Nope .Its not able to Commented Jan 17, 2018 at 5:35
  • Even i feel just 3rd like will be enough to select value from dropdown. Commented Jan 17, 2018 at 6:36

3 Answers 3

1

If your select tag is visible in DOM without you clicking the dropdown, just use the third line

Select select = new Select(driver.findElement(By.cssSelector("select")));
select.selectByValue("QA");

Otherwise, click on the dropdown which will probably generate the DIV with class

class="menu transition hidden"

and then you can use the select line.

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

Comments

1

U don't really need to click on dropdown. U need to find dropdown as WebElement, convert it to SelectElement and use it's class method to select option.

I assume, that in provided code you click on dropdown, then click on opened fields with options. But I think it's a lot of unnecessary actions. Unless you have any scripts, triggered by that clicks, which must be tested.

Comments

-1

From your description it sounds like the SELECT element isn't visible until the 2nd click happens. It's also possible that the 2nd click creates the SELECT element via Javascript. You could investigate this by using the Chrome devtools and search for the SELECT element before the 2nd click happens. See if it exists or maybe exists but is not visible. Then click on the 2nd element and see how the HTML changes. Without access to the site, that's about as good an explanation as we can provide. It's not something I would worry about. It's just the way the site is designed.

12 Comments

This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. - From Review
it does address them. i would expect a straightforward answer looks like "the use of that second line of code is ..." and " After clicking to the dropdown we cant we directly select the required element because ..."
@JeffC you clearly misunderstand how Stack Overflow works. Your post ended up in the "Low Quality Posts" queue and i was given the option to review it. I initially judged it was suitable as a comment instead of an answer, so i recommended it for deletion. Per your comment, i gave it a second review, and i still believe it does not answer the question, so i kept my initial review. I did not read other answers and i won't unless they end up in my review queue. Are there incorrect and/or non suitable answers there ? maybe, but that is not the point.
@JeffC If you see incorrect answers, then downvote them. If you are unhappy with my recommendation, then flag it and ask a moderator to have a look at it. The fact you got zero upvote, no acceptance from the OP and two recommendations for deletion should tell you this post does not meet the standards of Stack Overflow. I might add you should consider deleting it, and post a crystal clear answer that will be hopefully well received and accepted.
|

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.