WebDriver SelectElement get selected value - testing

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

Related

How to select value from Google auto location using Selenium

How to automate this to select particular value even the dropdown list id cannot be inspected. Can anyone help me out on this?
Need to select U.S. 22 Imperial from the list
Please find the HTML snippet
I am unable to proceed more than this. Please help me out!
WebElement location = driver.findElement(By.id("selectbox-city-list2"));
location.sendKeys("us");
You could use sendKeys and send arrow down to select an option. Selecting one by one with the arrow down will highlight the value. You will be able to check the highlighted value using the CSS class of highlighting.
You can use ActionClass
using this you can move your cursor over a specific element based on coordinates and perform a click.
1.So taking the coordinates of that text box.
2.Enter the full value in the text box. ,
3.calculate a very near coordinate to that text box(so that it will be the suggestion) and perform a click.
element = xxxxxxx;
Actions builder = new Actions(driver);
builder.moveToElement(element, x_coord, y_coord).click().build.perform();

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

Checkbox does not get checked

Hi I am automating a webpage which contains multiple checkbox.
It clicks on a some checkbox and misses some checkbox.
This is my code.
Should I put a wait statement before the click to avoid this problem.
IWebElement ClickElement = Wait.Until((d) => webDriver.FindElement(By.Id(parameter1)));
ClickElement.Click();
Can you try making this change in your code -
In wait until function you are checking if the element exists by using findElement(By.Id(parameter1))
After finding the WebElement check if this is displayed by using isDisplayed() method with in the waitUntil function.
You can also check if it has already checked or not by using isSelected() method.