When I was reading about driver.getWindowHandles(), somewhere I read 'it will return the window handles in the order how they were opened'.
However, when I use it in realtime, it's not the case.
public void checkAllWebpages() {
ArrayList<String> tabs = new ArrayList<String>(driver.getWindowHandles());
for(int tab = 1; tab<=5; tab++) {
driver.switchTo().window(tabs.get(tab));
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
When I run the above code, WebDriver loops through opened tabs however, it goes from last to first instead of first to last(Goes from right to left instead of left to right).
Let's say I have only one tab opened right now and let's call it 'a'. Now I open 5 tabs in order 'b', 'c', 'd', 'e', 'f'.
If I run the above code now, WebDriver will loop through the tabs in the order 'f', 'e', 'd', 'c', 'b', 'a', instead of 'a', 'b', 'c', 'd', 'e', 'f'. Why is this?
"I read somewhere.. why does it actually work in other way than I read somewhere" - that's kind of bad question here.
The order in which window handles are returned is defined in the specification that states:
The order in which the window handles are returned is arbitrary
which means there is no special order. It is up to a particular webdriver how to order the handles.
Related
Now I want to select first value from a drop down then perform some actions on it, then I want to select second value from the same drop down and perform the same action on it.
Here is my code:
WebElement bldgs=Fn_GetWebElement(CreateSSIObject.getProperty("Bldgselect"));
Select Bldg_select=new Select(bldgs);
List<WebElement> dropdownvalues = Bldg_select.getOptions();
int count=dropdownvalues.size();
System.out.println("Total number of values are :"+count);
for(int i=1;i<count;i++) {
if(dropdownvalues.get(i).isEnabled()) {
Bldg_select.selectByIndex(i);
System.out.println("Not Working :"+i);
waitForWebPagetoLoad(2000);
WebElement search_BTN=Fn_GetWebElement(CreateSSIObject.getProperty("search_Btn"));
fn_Click(search_BTN);
WebElement add_VEND=Fn_GetWebElement(CreateSSIObject.getProperty("add_vendors"));
fn_Click(add_VEND);
WebElement vendorName=Fn_GetWebElement(CreateSSIObject.getProperty("vendor_Name"));
fn_Click(vendorName);
vendorName.sendKeys(vendor);
waitForWebPagetoLoad(5000);
WebElement search_BTN1=Fn_GetWebElement(CreateSSIObject.getProperty("search_Btn"));
fn_Click(search_BTN1);
WebElement selectVendor=Fn_GetWebElement(CreateSSIObject.getProperty("select_Vendor"));
fn_Click(selectVendor);
WebElement addToSite=Fn_GetWebElement(CreateSSIObject.getProperty("AddTo_Site"));
fn_Click(addToSite);
}
}
here I am seaching for an element(basically drop down id) and then selecting each value with selectbyindex with i for loop. and then I am clicking on a button and performing some more actions on it. Now it is selecting only first value and doing all above stuff. But it is not going back in for loop to select 2nd value and performs same steps.
I don't quite understand your problem butI can see 2 issues that may be adding to confusion.
Index should 0 based
Your loop is starting with i set to 1. As lists are zero based indexes, you should start at 0
Referencing stale elements??
You are extracting the dropdown values outside of the loop and then referencing these within the loop using index. However, you are performing a lot of actions and events within each iteration.
You may be better extracting the values again within each iteration to ensure all your references are up to date and have not gone stale.
Can you please try below solution? I am not sure what action you are tryig to execute based on selection but I think below code solve your problem.
Select drpCountry = new Select(driver.findElement(By.name("Locator")));
List <WebElement> elementCount = drpCountry.getOptions();
int iSize = elementCount.size();
for(int i =0; i<iSize ; i++)
{
String sValue = elementCount.get(i).getText();
System.out.println(sValue);
drpCountry.selectByIndex(i);
if(sValue.equalsIgnoreCase("Selection1")){
//code to be executed if condition1 is true
}else if(sValue.equalsIgnoreCase("Selection2")){
//code to be executed if condition2 is true
}
else if(sValue.equalsIgnoreCase("Selection3")){
//code to be executed if condition3 is true
}
else{
//code to be executed if all the conditions are false
}
}
I want to know how to select a specific value from dropdown from a pool of values.
My logic is
1.Open the page
2.Fetch all the values from dropdown in list
3.Use a loop
4.Look for that value
5.If the value is not there,select the value x
For me it is saying No such Element Exception
Is it do we need to focus on that element first
In my code i want to select Nextran Corporation
Below is my code
#Test(dependsOnMethods={"go2personalsettings"})
void setru()
{
driver.switchTo().frame("contentFrame");
Select rudropdown=new Select(driver.findElement(By.id("DefaultOrganisationDropDown")));
List<WebElement> drop=rudropdown.getOptions();
int e=drop.size();
String actual_ru="999425, NEXTRAN CORPORATION - JACKSONVILLE";
for(int i=0;i<e;i++)
{
String expected_ru=drop.get(i).getText();
if(!expected_ru.equals(actual_ru))
{
rudropdown.selectByValue(actual_ru);
}
}
Try This:
WebDriverWait wait = new WebDriverWait(driver, 15);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//li[contains(text(),'" + ElementValue + "')]")));
WebElement webElement = driver.findElement(By.xpath("//li[contains(text(),'" + ElementValue + "')]"));
webElement.click();
The logic in your code doesn't match what you stated was your intent. You stated that you want to select a particular value from the dropdown, if it exists. If it doesn't, select value 'x'. Your code doesn't ever select value 'x' so I'm not sure what 'x' is but you can fill that in below. Basically the code attempts to select the expected value. If that expected value does not exist, it will throw an exception which is caught and then 'x' is selected. If it does exist, the value is selected and selecting 'x' is skipped.
driver.switchTo().frame("contentFrame");
Select rudropdown = new Select(driver.findElement(By.id("DefaultOrganisationDropDown")));
String actual_ru = "999425, NEXTRAN CORPORATION - JACKSONVILLE";
try
{
rudropdown.selectByValue(actual_ru);
}
catch (NoSuchElementException e)
{
// expected value does not exist, select the value x instead
rudropdown.selectByValue("x");
}
There are several solution, but nothing with hashmap and enum.
Website table element simple tbody, tr,td. The code below, I have webelements in the rows list, or the row data as a string in the other list.
List<WebElement> rows = table.findElements(By.tagName("tr"));
List<ArrayList<String>> rowsData = new ArrayList<ArrayList<String>>();
for(WebElement row:rows){
List<WebElement> rowElements = row.findElements(By.tagName("td"));
ArrayList<String> rowData = new ArrayList<String>();
for(WebElement column:rowElements){
rowData.add(column.getText().toString());
}
rowsData.add(rowData);
}
One row can be:
12, ab, apple, red, -1000;
66, ac, grape, blue, 1000; etc.
I have to check all the rows and find the first row which is red, apple and last row is not bigger than 0 and click.
More useful option can be enum and hashmap. Hashmap keys can be enum values, like ID, NAME, FRUIT, COLOR, NUMBER. Checking hashmap has COLOR= red, name = apple etc.
Any solution for this? I think it can be very useful for everybody!
Thanks in advance!
Have you thought of creating row as an object? So something like this
public class Fruit {
private String name;
private int id;
//go horizontally with your column names;
//have getters/setters here
}
Now your code could be written slightly differently like,
List<WebElement> rows = table.findElements(By.tagName("tr"));
List<Fruit> fruits = new ArrayList<Fruit>();
for(WebElement row:rows){
List<WebElement> rowElements = row.findElements(By.tagName("td"));
Fruit fruit = new Fruit();
//you know first column is name or so on
//or store column header order and use it here
fruit.setName(rowElements.get(0).getText());
fruit.id(rowElements.get(1).getText());
fruits.add(fruit);
}
Now you can iterate over fruits
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); }
I have created a map in MVEL and I have to iterate over it using foreach. How would I do that?
There is a similar question:
How to iterate over a map in mvel
But in that case the map was created in Java and had a method to return array of keys (entrySet) which is not the case with me.
//MVEL
map = [
'a': 'a1',
'b': 'b2',
'c': 'c3'
];
foreach (key: map) {
System.out.println(key);
}
I have tried both map and map.entrySet in the foreach loop but none seems to work.
Note: I test it using MVEL command line and using MVEL version 2.2.0.15
Although you have accepted an answer, I think it is better to add something as not to mislead other people:
... had a method to return array of keys (entrySet) which is not the case with me
First, Map is a Map. Map created in MVEL is simply a "Java" Map. The way to iterate is just the same and they are providing same methods
Second, entrySet() is not returning "array of keys". It is returning a Set of Entry (as its name suggests).
I am not sure why you cannot use entrySet as it works just fine for me. I suspect you have do foreach (e : map.entrySet). That will not work, because in MVEL, property navigation can mean several thing, like bean properties (which means it will call map.getEntrySet()), or looking up a map (which means it will call map.get('entrySet')), or getting the field (which means 'map.entrySet'). However all these are not valid for your case. You simply want to invoke map.entrySet() method so that you should just do foreach (e : map.entrySet())
The proper way to do is something like this:
map = ['a':'a1', 'b':'b1'] ;
foreach(entry : map.entrySet()) {
System.out.println('key ' + entry.key + ' value ' + entry.value)
};
You can use something like this:
map = [
'a': 'a1',
'b': 'b2',
'c': 'c3'
];
foreach (key : map.keySet()) {
System.out.println("Key:" + key + " Value:" + map[key]);
}
It outputs:
Key:b Value:b2
Key:c Value:c3
Key:a Value:a1