Having issue in selecting drop down element under makemytrip website - selenium

Having issue in selecting drop down element under the following website
http://flights.makemytrip.com/makemytrip/fareCal.do?intid=NewHP_to_DF_FC_Menu
I'm unable to select any one of the cities listed below.
Please help me out resolving the same.
Scenarios Tried
driver.findElement(By.className("chzn-single")).click();
driver.findElement(By.xpath("//span[contains,'NewDelhi']")).click();
driver.findElement(By.xpath("//span[#id='fromcity_chzn']")).click();

This works:
WebElement leavingFrom = driver.findElement(By.xpath("//*[#id='fromcity_chzn']/a"));
WebElement goingTo = driver.findElement(By.xpath("//*[#id='tocity_chzn']/a"));
leavingFrom.click();
leavingFrom.sendKeys("Bangalore");
leavingFrom.sendKeys(Keys.RETURN);
goingTo.click();
goingTo.sendKeys("Goa");
goingTo.sendKeys(Keys.RETURN);

Here is working sample:
//First get main dropDowns
var leavingFromDropDown = driver.FindElement(By.Css("#fromcity_chzn"));
var goingToDropDown = driver.FindElement(By.Css("#tocity_chzn"));
//Select value from first dropDown using dropDown items index
//First click on dropDown to open it
leavingFromDropDown.click();
//Now find items in it and click on any item using it's index (also can be used method to access this elements using their names
leavingFromDropDown.FindElements(By.Css(".active-result"))[1].click();
//this dropDown closes automatically, but if not you need to click on it again to close
//Same perform with second element
goingToDropDown.click();
goingToDropDown.FindElements(By.Css(".active-result"))[2].click();
If you want to use input box to enter any value from DropDown you need to find that element and using sendKeys set it's value. E.g.:
leavingFromDropDown.click();
var input = leavingFromDropDown.FindElement(By.Css(".chzn-search > input"));
input.sendKeys('Goa');
input.sendKeys(Keys.Enter);//or tab

Related

Can we Traverse on another page when executing foreach loop?

There is Table on the page, I do need to retrieve information row by row. That is handle by for-each. Execution on same page for retrieving information is working fine.
But, Can we handle Detail page of particular Row ? Here "Test 1" has its own detail page. There are some operation which need to handle on detail page. How can we manage it and get back to actual execution.
As of now I am dealing it with for-each loop, and getting StaleElementReferenceException which is correct as webElement lost its actual ListElement<WebElement>
List<WebElement> findList = driver.findElements(By.xpath("//a[#class='*****']));
for (WebElement webElement : findList) {
...
//detail page is access by clicking webElement
webElement.click();
findList = driver.findElements(By.xpath("//a[#class='*****']));
}
If there is any way, Please suggest. Thanks.
You can do something like this :
List<WebElement> findList = driver.findElements(By.xpath("//a[#class='*****']));
for(int i = 1 ; i<=findList.size() ; i++){
driver.findElement(By.xpath("(//a[#class='***'])[" + i + "]")).click();
// extract some data from next page which is invoked by clicking on Test1 hyperlink.
}
in this way every time you are looking for //a[#class='***'][i] web element i , starting from 1 and till the end point.
P.S : You need to check this in devtool //a[#class='***'][1], whether it indicates the Test1 hyperlink or not.
Hope this helps.
Try to collect urls to detail pages of all and then open them.
There are two scenarios i can think of :
Clicking the link e.g. Test1 opens a new tab :
In this case you can just switch to new tab inside your loop , finish your operation on this tab and finally close the second tab / switch to first tab before continuing with your next iteartion. This should also not cause StaleElementException.
Clicking the link e.g. Test1 redirects to some other url in same tab :
For each iteration of loop, click on target link , perform some operation on new page.Before you continue with next iteration , Navigate to page containing your table again and lookup all the row elements again. Now , for next iteration you won't get StaleElementException. You will want to use the for loop so that you can maintain the index outside instead of a foreach variant.
findList = driver.findElements(By.xpath("//a[#class='*****']))
I would recommend the first option since that would be more clean and you can perform a shift + click on your link e.g. Test1 to force it open in a new tab instead of redirecting in same page. You can perform a shift + click using "Actions api" available in Selenium.

Tosca: How to scan Dropdown textbox which disapper upon opening xScan

I have a problem in scanning a drop-down menu which disappears upon opening the xScan. I need to get the module id of the dropdown menu to verify some test steps.
Do you have any solution with this if it is not really possible to get the module id of the dropdown menu?
Open developer tools in your browser of choice (F12), navigate to the console and input the following code:
var fulldoc='';
var scrollX=0;
var scrollY=0;
document.addEventListener("keydown",function(event){
if(event.key=='q' && event.altKey){
fulldoc=document.body.outerHTML;
scrollY=window.pageYOffset;
scrollX=window.pageXOffset;
}
if(event.key=='w' && event.altKey){
document.body.outerHTML=fulldoc;
document.body.scrollTo(scrollX,scrollY);
}
});
When the window looks the way you would want to scan, press 'Alt + Q', then press 'Alt + W'.
Now your window will freeze and then you can scan your page.
To steer the objects you need to refresh your browser.
You can resolve the issue with below 2 steps
1 - Add some text in textbox which will populate the dropdown below it .
2 - Use Send Keys Module to scroll down and select the value.
I had a similar issue where we had a popup that only appeared when clicking on a text box. The solution we received from the Tricentis trainer was as follows:
Part One
1. Open your application in Chrome
2. Right click the inspect
3. In the inspector window, on the Elements tab, navigate to your html element where it should be (you can do that by clicking on the element and check that you can see the html in the element)
4. Use the debugger to add a break point there, this should pause it and you should be able to see the elements you need to steer it.
5. Once you found the element, you will need the type of element (e.g. div, span, etc), and the class name
Part two
1. Rescan your module and select any element that matches the criteria of your element selected in Part One #5
2. Identify it by only it's class name property and tag
3. Save and close
4. Edit the element in the module view by changing the class name. This should help you steer it
Note: if the element class name is not unique, you might need to use Explicit name.
Good luck

Selenium Object Identification Issues

I have a combo box with 2 values(say Xreg and MBA).
By default only one value(either Xreg or MBA) will be displayed based on a search criteria..
The xpath for Xreg is
/html/body/div/div[4]/div[2]/form/div[1]/table[2]/tbody/tr[1]/td[2]/select/option[2]
and for MBA
/html/body/div/div[4]/div[2]/form/div[1]/table[2]/tbody/tr[1]/td[2]/select/option[3]
How do i capture the default value on page load. It might be either of them and each time i want to capture the value that is displayed by default in the combo box
You can use Selenium's Select class for this:
// this is only an example with the code provided, usually the select element has an id and you wouldn't necessarily need xpath
By locatorToYourSelectElement = By.xpath("/html/body/div/div[4]/div[2]/form/div[1]/table[2]/tbody/tr[1]/td[2]/select");
WebElement selectElement = driver.findElement(locatorToYourSelectElement);
Select dropdown = new Select(selectElement);
// Supposing you do not have multiple selection you will get the displayed element now very easily:
WebElement currentlySelectedOption = dropdown.getFirstSelectedOption();

How to traverse all the element of dropdown in Selenium WebDriver?

I have a drop down on my web page and I have to traverse this drop down and select the specific one.But its not so simple, In this the traversing should be show by the hover and at last the specific element or item should be selected.(The mouse hover action should start from the first element till the required element and then select the element).
You need to look into Select, there are a couple of different ways of selecting elements from the dropdown, below is one example.
Select dropdown = new Select(<WebElement>);
dropdown.selectByVisibleText("text");

Verifying current selection of a dropdown menu

Using Selenium Webdriver (Java)
I need to verify that a selected value from a dropdown menu is saved when a user returns to that page in another session.
The dropdown is simple, it just changes the number of records displayed per page after a search.
The values are 5, 10, 25, 50 and 100.
Using getText() simply returns the values in the array. I need to verify that if a user chooses, say, 25, that that value is the same when returning to this particular page. A simple assert statement doesn't work here because the value "25" can be present in other fields.
I've also tried various uses of Select without success.
I'd appreciate any suggestions. If I've missed any key information, feel free to point it out and I will update the post.
You need to use Select class
IWebElement selectElement = driver.FindElement(By.Id("id"));
SelectElement selectedValue = new SelectElement(selectElement);
string selectedText = selectedValue.SelectedOption.Text;
Mine written in C#. But Java is fairly close as well.See this
EDIT:
you should be using getFirstSelectedOption().getText() instead of SelectedOption.Text; according to the api doc
checkout the following answer
Select select = new Select(driver.findElement(By.xpath("//path_to_drop_down")));
WebElement option = select.getFirstSelectedOption()
-- This will give the selected option in dropdown
Select select = new Select(driver.findElement(By.xpath("//path_to_drop_down")));
WebElement option = select.getFirstSelectedOption()
String SelectedText = option.getText();