0

I have the following code:

<span class="dropDown">Choose</span>
<div class="dropdownQuestions active" style="display: block; max-height: 310px;">
<span data-index="0" data-value="1" onclick="gt.setQuestion(this, true);">Warrior</span>
<span data-index="1" data-value="2" onclick="gt.setQuestion(this, true);">Mage</span>
<span data-index="2" data-value="3" onclick="gt.setQuestion(this, true);">Sorcerer</span>

And what I want, if possible, is a list of the the drop down list so after I can use one of them to click, I have searched a lot and could not find any answer that would help me.

Other option would be to click using the data-index, or data-value String. the last option I would want is clicking by the String "Warrior", "Mage" or "Sorcerer", but if there is no other way, Ill be glad with that.

thanks!

Tiago Machado
  • 355
  • 7
  • 24

2 Answers2

1

After clicking on the dropdown, you can get all the dropdown options in a list and later use them as you want(looping and selecting the desired option with specific condition or getting option by index).

  List<WebElement> options= driver.findElements(By.xpath("//span[@class='dropDown']/div/span"));
  options.get(1).click(); // here 1 is the index value of the option to select or you can use loop if looking for some specific condition

For the image link you shared in the comment, try the following cssSelector:

List<WebElement> option= driver.findElements(By.cssSelector("div.questionDropdownOptions.activeSelectMenu span"));
                option.get(1).click();
    }

Note: Please import below packages in your code:

import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
mkhurmi
  • 786
  • 4
  • 12
  • Thanks for the answer, but im getting "InvalidSelectorException" The dropDown appears when I click, the div class is the one that has the chooses inside(spans). I just started using selenium so not sure... I inspected the code and copied the xpath and added it to your code example... I have this image as example: https://ibb.co/1XrdRRf Thats what I want, the list of coiches inside that div, for me to use them as I please later. As I said I tried do copy the xpath/full xpath from that div to your example. but no luck Thanks for your time! – Tiago Machado Apr 20 '20 at 20:19
  • @TiagoMachado InvalidSelectorException occurs when there is syntax error in code. Because I was not on my pc I replied via phone. I will check and get back to you in a while. – mkhurmi Apr 20 '20 at 20:25
  • @TiagoMachado , I have updated my answer based on your comment. Please check if it solves your problem. – mkhurmi Apr 20 '20 at 20:47
  • wow! I dont know how to thank you! working perfectly now!!!!! THANK YOU! ^_^ – Tiago Machado Apr 20 '20 at 21:01
0

I think that all what are you looking for has already been answered here:

-> How to select a dropdown value in Selenium WebDriver using Java

and if you want to iterate through it, this might be helpful

-> How to count the number of options in a select drop down box in Selenium WebDriver using Java?

To be more specific, in a first link, you can see that you can click on the dropdown option using String parameter and also you can use an index.

I know you were probably looking for a specific code, but sadly, I am not on a computer, but I am pretty sure that this answer's your question.

Good luck with your code :)

StyleZ
  • 1,276
  • 3
  • 11
  • 27
  • Hey thanks for the answer, but saddly, Select only works with a select tag present, as answered here: https://stackoverflow.com/questions/28322816/driver-info-driver-version-unknown-at-org-openqa-selenium-support-ui-select-i – Tiago Machado Apr 20 '20 at 20:08