How to take all the dropdown options? - selenium

In selenium 2.0, i am trying to get the list of drop down values and to print it. How to do this? I am trying below:
for (int i = 1;i<=13;i++)
{
WebElement values=driver.findElement(By.xpath("//li[#rel='i']/a/span[#class='pull-left']"));
System.out.println(values);
}
#rel= '1', '2' should go like this.. so that i can print all the values.
But this is not working.. How to use the 'i' in this element.
Thanks

Instead of hard coding no of options value you can get that dynamically.
List<WebElement> options = driver.findElements(By.xpath("//ul/li/a/span[#class='pull-left']"));
//iterate above list to get all option values
for(WebElement eachOption : options) {
System.out.println(eachOption.getText());
}

Some how i managed to get this..
for (int i = 1;i<=13;i++) {
//System.out.println("//li[#rel=" + i +"]/a/span[#class='pull-left']");
String values=driver.findElement(By.xpath("//li[#rel=" + i +"]/a/span[#class='pull-left']")).getText();
System.out.println(values); }

Related

Limit the element to be stored in List<WebElement> - selenium/java

I need to know how to store the first 10 values in List<WebElement> instead of storing all the elements that is present?
Right now my code stores all the elements:
By mySelector = By.xpath("/html/body/div[1]/div/section/div/div[2]/form[1]/div/ul/li");
List<WebElement> myElements = driver.findElements(mySelector);
for(WebElement e : myElements) {
System.out.println(e.getText());
}
Try using position() with the li.
By.xpath("/html/body/div[1]/div/section/div/div[2]/form[1]/div/ul/li[position() < 11]")

Dropdown duplicate value automation using Selenium

How to check the duplication of values in a checkbox using Selenium Webdriver
something like the below one will work if both the options have same value
public boolean isSelectOptionsRepeating(WebElement dropdown)
{
Select s = new Select(dropdown);
List<WebElement> list = s.getOptions();
Set<String> listNames = new Hashset<String>(list.size());
for (WebElement w : list) {
listNames.add(w.getText().trim());
}
if(list.size()== listNames.size())
return true;
else
return false;
}
You can store the values of drop down in String array and
traverse string array and use Hashmap for storing the values from the dropdown and if duplicate occurs increement the count by one
voila......you would know the the Values with its count, if count > 1. Duplicate
for reference : Java Beginner - Counting number of words in sentence

How to show the selected options from a multi select drop down using selenium java?

I'm trying to show all the selected options from a multi select drop down list. But not getting the proper way to do this. Please help me on this.
Here is the html code for the drop down:
<select multiple id="fruits">
<option value="banana">Banana</option>
<option value="apple">Apple</option>
<option value="orange">Orange</option>
<option value="grape">Grape</option>
</select>
Here is the code i'm trying with:
public void dropDownOperations()
{
driver.get("http://output.jsbin.com/osebed/2");
Select DDLIST = new Select(driver.findElement(By.id("fruits")));
DDLIST.selectByIndex(0);
String currentvalue = DDLIST.getFirstSelectedOption().getText();
System.out.println(currentvalue);
DDLIST.selectByIndex(1);
String currentvalue1 = DDLIST.getFirstSelectedOption().getText();
System.out.println(currentvalue1);
}
I also tried with this code:
Here i'm getting this output:
[[[[[ChromeDriver: chrome on XP (69aee19e9922ca218ff47c0ccdf1bbbc)] ->
id: fruits]] -> tag name: option], [[[[ChromeDriver: chrome on XP
(69aee19e9922ca218ff47c0ccdf1bbbc)] -> id: fruits]] -> tag name:
option]]
public void dropDownOperations1()
{
driver.get("http://output.jsbin.com/osebed/2");
Select DDLIST = new Select(driver.findElement(By.id("fruits")));
DDLIST.selectByIndex(0);
DDLIST.selectByIndex(1);
List<WebElement> currentvalue1 = DDLIST.getAllSelectedOptions();
System.out.println(currentvalue1);
}
Your second approach should work fine with a minor fix. getAllSelectedOptions() will return a List of selected options as WebElement. You need to iterate over the list to get the text from WebElement.
List<WebElement> selectedOptions = DDLIST.getAllSelectedOptions();
for (WebElement option : selectedOptions){
System.out.println(option.getText());
}
Try This:
List<WebElement> allSelected = select.getAllSelectedOptions();
Iterator itr = allSelected.iterator();
while(itr.hasNext()){
WebElement item = (WebElement) itr.next();
System.out.println(item.getText());
}
Try this below code, It will select one by one options from dropdown.
Select DDLIST = new Select(driver.findElement(By.id("fruits")));
DDLIST.selectByIndex(0);
DDLIST.selectByIndex(1);
List<WebElement> selectedOptions = DDLIST.getAllSelectedOptions();
for(int i=0; i<selectedOptions.size(); i++)
{
System.out.println(DDLIST.getOptions().get(i).getText());
}
Try This:
System.out.println(DDLIST.selectByIndex(0).getText());
System.out.println(DDLIST.selectByIndex(1).getText());
And so on. Instead of using a variable and trying it.
Try this:
Select DDLIST = new Select (driver.findElement(By.id("fruits")));
for(int i=0; i<DDLIST.getOptions().size(); i++)
System.out.println(DDLIST.getOptions().get(i).getText());

Incrementing value with Selenium IDE

How do I increment value of img path when said path looks like this?
//ab[x]/img
X value increasing by 1 and has a limit of 50.
Trying to write a test case on how to click on several images on website.
Edit: Just wanted to add that I'm just starting with Selenium IDE and using standart commands.
Solution 1: Format your xpath path selector
for(int i=1; i<=numberOfImages; i++) {
String path = String.format("//ab[%d]/img", i);
WebElement image = driver.findElement(By.xpath(path));
if(image != null) {
image.click();
}
}
Solution 2: Select all elements that "//ab/img" returns and iterate over them.
String path = "//ab/img";
List<WebElement> imgElements = driver.findElements(By.xpath(path)); //notice the plural
for(WebElement image : imgElements) {
image.click();
}

How to perform Xpath looping?

While working on selenium,I have the xpath
//tbody[#id='se-tbody-result']//tr[3]//a
I want to loop tr[value] from tr[3] to tr[20].
Such xPath would return you values from tr[3] til tr[20]
//tbody[#id='se-tbody-result']//tr[position()>=3 and position()<=20]//a
Simple for loop in java works for me
public void sample() {
for (int i=3; i<=20; i++) {
driver.findElement(By.xpath("//tbody[#id='se-tbody-result']//tr["+ i + "]//a"));
}
Use like below if in python-
trs = driver.find_elements_by_xpath("//tbody[#id='se-tbody-result']//tr")
for i in trs[3:20]:
my_a = i.find_element_by_xpath(".//a")
print my_a.get_attribute('href')