Selenium Object Identification Issues - selenium

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();

Related

selenium send key for xpath not working

I want make automation for this web site
in this web site 3 text box are here check image
1st text box x path is /html[1]/body[1]/div[3]/div[1]/div[2]/div[1]/searchbar[1]/div[1]/div[1]/div[1]/div[1]/form[1]/div[1]/div[1]/div[1]/input[1]
here is my code
driver.findElement(By.xpath("/html[1]/body[1]/div[3]/div[1]/div[2]/div[1]/searchbar[1]/div[1]/div[1]/div[1]/div[1]/form[1]/div[1]/div[1]/div[1]/input[1]")).sendKeys("rio salon");
when I run this code I got this error
Exception in thread "main"
org.openqa.selenium.ElementNotInteractableException: Element is not reachable by keyboard
How can i fix it? I hope my xpath is correct.
The field has aria-hidden=true attribute, so you get ElementNotInteractableException. You need to click on the dropdown first to change the attribute to true
WebElement dropdown = driver.findElement(By.xpath("//*[#id='search-form']/div/div[1]/div/div[1]/span"));
dropdown.click();
WebElement textField = dropdown.findElement(By.xpath("./parent::div/following-sibling::input[contains(#class, 'ui-select-search')]"));
textField.sendKeys("rio salon");
You can click in an input field with a div or span tag, but you cannot type in the field. So, your XPath must be written with an input tag if you want to sendkeys or type in an input field. For example:
//input[contains(#placeholder,'Site')]

Get text associated with check box in selenium where html code does not have value or name property

I have a Radcombobox in my application which has several check boxes in the combo box. What I wish to do is to fetch the text associated with the check box. I searched on internet and came to know that text can be fetched from name or value property in HTML code, but problem is that my HTML code does not have such properties.
HTML code:
input class="rcbCheckAllItemsCheckBox" type="checkbox"
Check All
What i wish to do is to fetch value "Check All".
Using code x = driver.findElement(By.xpath("/html/body/form/div[1]/div/div/div/input")).getText();, value returned is blank.
You can get the text through JavaScript API of Rad Controls. You can check the official documentation- http://docs.telerik.com/devtools/aspnet-ajax/controls/combobox/client-side-programming/overview
Basically, you first locate the element in JS and then use the official control method, in your case get value method. You can also perform several other useful operations if you need to.
You can execute JS in WebDriver with the following code:
IWebDriver driver;
IJavaScriptExecutor js = driver as IJavaScriptExecutor;
string title = (string)js.ExecuteScript("return document.title");

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();

Having issue in selecting drop down element under makemytrip website

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

WebDriver SelectElement get selected value

var test = SelectElement([TheIWebElemement]);
Assert.AreEqual("55", test.SelectedOption.Text);
Based on the above snippet, does anyone know how to get the selected value in the dropdown. I am opening a form in edit mode, so I know the value I expect the selected option to have. I do not want the text, I want the value behind the option as the text I don't care about.
From my inspecting, all I can seem to get at is the text of the option.
The SelectedOption that is returned is simply the IWebElement representing that option.
Therefore, your question becomes 'how do I get the value of an option element?'. Since the SelectElement is kind and has given you the IWebElement for free, you can simply do:
var selectedValue = test.SelectedOption.GetAttribute("value");
Assert.IsNotNullOrEmpty(selectedValue);
Assert.AreEqual("the value", selectedValue);