Verifying current selection of a dropdown menu - selenium

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

Related

How to handle Autosuggestion text field having dropdown list in Selenium?

There is a text field called "Choose an Industry" on clicking inside it will show list of dropdown values.
Attaching the screen shot for ref
There are a couple issues with the code you posted. With a little clean up it may work but there are methods provided by Selenium via Select that you should take advantage of.
String expectedValue = "Adverstising Services";
Select dropDown = new Select(driver.findElement(By.xpath(".//*[#id='select2-chooseInd-container']")));
dropDown.selectByVisibleText(expectedValue);
You can read more about Select and the methods available in the docs, here: https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/support/ui/Select.html

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

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

How to manipulate user selected text using webdriver?

Lets say i have the following snippet in my web page:
<p> This is some text </p>
I want WebDriver to select "some" in this text, as if the user selected it. How should i do this? I know how to get the <p>-element:
WebElement editable = getDriver().findElement(By.id("someId"));
editable = editable.findElement(By.tagName("p"));
System.out.println(p.getText());
The println prints "This is some text".
I tried sending keys to the element, and that used to work(in selenium 2.0b), but i'm using selenium 2.6.0 now, and it stopped working:
editable.sendKeys(Keys.chord(Keys.SHIFT, Keys.LEFT));
Does anyone have ideas? I'm using the FirefoxDriver.
I did this once for Firefox using Javascript. Basically I used the range object in Firefox to select the text. Change the start and end range index based on what you want to select. This would not work in IE, because selecting range is conceptually different in IE. I don't have the IE code handy but since you are concerned about FF, you could give this a shot.
Let me know if you interested in IE text range.
String script = "var range = document.createRange();" +
"var start = document.getElementById('idofthedivthatcontainstext');" +
"var textNode = start.getElementsByTagName('p')[0].firstChild;" +
"range.setStart(textNode, 8);" +
"range.setEnd(textNode, 13);" +
"window.getSelection().addRange(range);";
((JavascriptExecutor)driver).executeScript(script);
You are trying to select the contents of the p tag by drag select right I am not sure if that is objectively possible to be done by the user as your are suggesting.. Selenium now tries to mock the exact action that a user can perform on a browser. thus you just cant send a shift and left select key on the p tag and expect it to select unlike a textbox where it is very much possible but you might probably have to click on the text box first to get it into focus.
Here is what I would suggest to achieve this, not sure if it will work.
a) send the left click on the p tag
b) hold the shift key
c) drag the mouse to the end of the p tag.
Hope that helps.
Use the .Text property of the IWebElement
WebElement editable = getDriver().findElement(By.id("someId"));
editable = editable.findElement(By.tagName("p").Text).ToString();
editable.Replace("This is ", "").Replace(" text.");
System.out.println(p.getText());