Selenium: Get current value from drop-down menu - drop-down-menu

I'm trying to find a simple Selenium call to grab the current option from a select drop-down list. I'm aware there are calls which grab all the values in a list but I wish to know which option is currently selected. Apologies if this is trivial but google and Selenium IDE didn't help me. Thanks.

You should be able to use the getSelected* commands to return the ID, index, or label of the selected item. Below is quoted from the Selenium Reference:
storeSelectedId ( selectLocator, variableName )
Gets option element ID for selected option in the specified select element.
Arguments:
selectLocator - an element locator identifying a drop-down menu
variableName - the name of a variable in which the result is to be stored.
Returns: the selected option ID in the specified select drop-down
storeSelectedIndex ( selectLocator, variableName )
Gets option index (option number, starting at 0) for selected option in the specified select element.
Arguments:
selectLocator - an element locator identifying a drop-down menu
variableName - the name of a variable in which the result is to be stored.
Returns: the selected option index in the specified select drop-down
storeSelectedLabel ( selectLocator, variableName )
Gets option label (visible text) for selected option in the specified select element.
Arguments:
selectLocator - an element locator identifying a drop-down menu
variableName - the name of a variable in which the result is to be stored.
Returns: the selected option label in the specified select drop-down

I would use storeSelectedValue or getSelectedValue
JUNIT
String value = selenium.getSelectedValue(selectLocator)
Selenium Action
storeSelectedValue ( selectLocator, variableName )

there is a link to practice such things:
"https://letskodeit.teachable.com/p/practice"
there's a "Select Class Example"
1.in this test first it clicks on "Honda" in the dropdown menu
2. then extracts the select-tag as parent of the option-tag "Honda"
3. then converts it to Select object
4. then i uses the getFirstSelectedOption() to compare it with the expected value "Honda.
#Test
public void selectTagDemo() {
WebElement hondaElement = webDriver.findElement(By.xpath("//option[#value=\"honda\"]"));
hondaElement.click();
WebElement selectCarWebElement = hondaElement.findElement(By.xpath("//parent::select"));
Select selectCar = new Select(selectCarWebElement);
Assert.assertEquals(selectCar.getFirstSelectedOption().getText(), "Honda");
}
if you need the whole Test class comment below

Related

Setting date value in an Interactive grid based on a page item

I am trying to set a interactive grid column value (which is a date) based on a page item (which is also a date). I have already tried defaulting and using dynamic action set value (jquery seletor) to set item value to the interactive grid column but it does not work how I want it to work.
I have a page item called "P_DEF_DATE" and I want to set a date column in the interactive grid to this value but I want when I change the value in the page item and I click add row on the interactive grid, it must always use whatever value I have in the page item. For example:
P_DEF_DATE = 12-JAN-2021
when I click on add row in the interactive grid, my date column must equal to P_DEF_DATE and i add a few rows based on that date but then i change the date of P_DEF_DATE to:
P_DEF_DATE = 28-JAN-2021
now I want when I click on add row in the interactive grid, I it must show this new date from the page item in the date column in the interactive grid, keeping in mind the page does not refresh and I have rows with the date 12-JAN-2021.
Thank you in advance!
I implemented same few days ago. Following is what I did.
Create Dynamic Action on Row Initialization Event, set Region to your IG
Set True Action to Execute JavaScript Code
Use code
var model = this.data.model,
rec = this.data.record,
meta = model.getRecordMetadata(this.data.recordId);
if ( meta.inserted ) {
model.setValue(rec,"COLUMN_NAME", $v("P_DEF_DATE"));
}
Replace JOB with your column name and P_DEF_DATE with you Item name
More details Here
Also, out of curiosity, why there is no number like P1, P2 in your item name ??

Select From List selects item but does not commit change

Using Robot Framework, I have a dropdown with multiple options. When I use the select from list option I can see the selection become highlighted, but when the dropdown closes, the value is not changed.
Here is a sample of my code:
click element id=month
select from list xpath=//select/#name="month" September
click button css=button.submit
I have tried variants of this with the select from list by label and select from list by value, and they fail with error stating the selected list or value does not exist.
Select from list by value example:
click element id=month
select from list by value xpath=//select/#name="month" September
click button css=button.submit
Select from list by label example 1:
click element id=month
select from list by label xpath=//select/#name="month" September
click button css=button.submit
Select from list by label example 2:
click element id=month
select from list by label xpath=//select/#name="month" label=September
click button css=button.submit
Anyone experienced this before where an item gets "selected" but the value does not get changed?
Use following keyword.
Pass locator as 1st argument and value as another argument
Select from list by label and validate
[Arguments] ${locator} ${select value}
Focus ${locator}
# Select from list by label
Select From List By Label ${locator} ${select value}
# Get selected value and validate it against passed value as argument
${value} = Get Selected List Label ${locator}
Should be equal ${select value} ${value}

Match text in table and click on it using selenium webdriver

I have one table in which there is one column called "state". I have to find and click on link which matches with text "closed successful" in that column. xpath for each cell in that column is
"//*[#id='TicketID_xxxxxx']/td[7]/div
Where xxxxxx are numbers of tickets. So how can I find matching text with different xpath values and click on it using selenium webdriver. Please help.
(Optional- I have to click on that element, click on back and find next element with same matching text)
Hi Sanket Patel please do it like below
driver.get(yourWebPageLInk); // link to your web-table web page
// take all of the element under Column "State" inside list
List<WebElement> columVal = driver.findElements(By.xpath("//*[starts-with(#id,'TicketID_')]/td[7]/div"));
// count the size of the list to match with the size of the column state
System.out.println("Size of the contents in the column state is : " +columVal.size());
// now for matching one of the content and then performing some action please
// start a for loop
String oneVal = "closed successful";
for(int i=0;i<columVal.size();i++){
System.out.println("Content text is : " + columVal.get(i).getText());
// match the content here in the if loop
if(columVal.get(i).getText().equals(oneVal)){
// perform action
columVal.get(i).click();
}
}
You can use the text() method
String text = "someText";
driver.findElement(By.xpath("//div[contains(text(), '" + text + "')]")).click();
Or with cssSelector
String text = "someText";
driver.findElement(By.cssSelector("div:contains('" + text + "')")).click();
You can use the TableDriver extension to Selenium WebDriver for situations like this. (https://github.com/jkindwall/TableDriver.Java). It would be helpful if there was more detailed info about the table in question, however if we assume that the table has a column "link" containing the link you need to click, and we assume the id of the main table element is "tabelId" you could do it like this:
Table table = Table.create(driver.findElement(By.id("tableId"));
WebElement element = table.findCell("state=closed successful", "link").Element;
element.findElement(By.tagName("a")).Click();

MVC3 Set Drop down list Selected value

#Html.DropDownListFor(m => m.field, Model.fieldList, "Select")
I have the code above to get the values of a drop down list from a database. Because the database does not have the select value, I am add it to the list. However, I want to set the selected value to a certain value(variable). How can I do this?
Say I want the selected value to be "WI" and "WI" is in the list. How do i do this? Or how do I specify the #value property?
The DropDownListFor function will first look if there is a selected value in the provided select list. If there are no selected values in the selected list items, it will look at the value of the field that the drop down list is for and attempt to match that to the value of each select list item until it finds the correct one. If any select list item does not have value, it will compare it to the text instead.
Once you have the full list generated, loop through it and find the value you want selected, then set the selected property to true and it should choose that one.

Clear jqgrid toolbar when custom defaultvalue is set

I want to clear the toolbar of my grid, but not to the default value of the column. I want to empty all fields.
When I use the
$("#Jqgrid")[0].clearToolbar();
method the toolbar gets the initial default values..
You can choose one from the following two ways.
1) You can temporary change the defaultValue of the searchoptions to "" before call of clearToolbar. You can use setColProp method for example to change column properties (see en example here).
2) Set the value of the the toolbar element manually to "" or to any other value which you want. There are simple way how the ids of the input or select elements of the toolbar are constructed. Let us you have column with the name 'col1' (the corresponding column of colModel has name: 'col1'). Then the id of the element in the filter toolbar will be gs_col1. So you can use
$("#gs_col1").val("");
to clear the field. In more general case if the colname is the variable which hold the value from colModel[i].name you can use
$("#gs_" + $.jgrid.jqID(colname)).val("");

Resources