I need to edit a dropdownlist item value using c# and selenium.
Here is my code IWebElement objSelect = driver.FindElement(By.Name("classes")); var selectElement = new SelectElement(objSelect); selectElement.SelectByValue("class-55");
I need to edit value "calss-55" to be example "class-a11".
Thanks
Related
I am not so into .NET and SharePoint and I have the following doubt working on a Web Part into a SharePoint 2013 project.
Into my web part I have a dropdown like this:
DropDownList dropDownnEtichetta = new DropDownList();
Then I add some items to this DropDownList in this way:
for (int i = 0; i < etichettaCorrenteList.Items.Count; i++)
{
dropDownnEtichetta.Items.Add(new ListItem(valoreDaMostrare, valoreId));
}
It works fine, as you can see the ListItem is populated passing the text shown in the dropdown and the value.
I want to know if is it possible to add a third information to this list item. In case what can I do?
https://learn.microsoft.com/en-us/dotnet/api/system.web.ui.webcontrols.listitem.-ctor?view=netframework-4.7.2
shows that ListItem has not a constractor that takes three string parameters. It ha only
ListItem()
ListItem(String)
ListItem(String, String)
ListItem(String, String, Boolean)
these constructors.
For ypur purpose you can extend ListItem Class.
Solved by myself in this way:
ListItem currentItem = new ListItem(valoreDaMostrare, valoreId);
currentItem.Attributes.Add("valoreDaStampare", valoreDaStampare);
dropDownnEtichetta.Items.Add(currentItem);
I have created a checkboxlist and dynamically it displays value of the list.
But I need to design as per the image shown. What css class or which option to be used to achieve this.
listcontents.Add(thisQ.Option.ToString());
//CheckBoxList CB = new CheckBoxList();
CheckBoxList1.DataSource = listcontents;
CheckBoxList1.DataBind();
I have a form that automates tasks on a page by user input but I'm having trouble interacting with an element on the page. It's a CheckBoxList with dynamic names and number of elements. The HTML looks like this:
<ol id="ratingModification_SupplierContact_content">
<label><input type="checkbox" name="searchQuery.vehicleGroups[0].isSelected" value="on" class="vehGrp"> abcd ef (ghi)</label> <br>
<label><input type="checkbox" name="searchQuery.vehicleGroups[1].isSelected" value="on" class="vehGrp"> jklm no (pqr)</label> <br>
</ol>
Where " abcd ef (ghi)" is the label of the first checkbox.
I already have a button that extracts the labels from the elements and puts them in an array designed with help from users here:
var vehicleGroupInputElements = Builder.Driver.FindElements(By.ClassName("vehGrp"));
var vehicleGroupNames = vehicleGroupInputElements.Select(f => f.FindElement(By.XPath(".."))).Select(f => f.Text).ToArray();
And I populate my form's CheckedListBox with:
vehicleGroupList.Items.AddRange(vehicleGroupNames);
But when I try to send the user selection back to the page I run into issues. I have tried selecting based on index via IndexOf() and the ClassName but can't figure out the syntax to make it work. Failed example:
foreach (int userChecks in vehicleGroupList.CheckedItems)
{
int checkIndex = vehicleGroupList.Items.IndexOf(userChecks);
var checkTarget = Builder.Driver.FindElements(By.ClassName("vehGrp"));
checkTarget.IndexOf(checkIndex).Click();
}
Which won't compile because int checkIndex cant convert to an IWebElement. I have also tried to build a string to address the index with xpath but it can't find the element or throws a no compound names exception. Failed example:
foreach (int userChecks in vehicleGroupList.CheckedItems)
{
int checkIndex = vehicleGroupList.Items.IndexOf(userChecks);
string elementTarget = "searchQuery.vehicleGroups[" + checkIndex + "].isSelected";
var checkTarget = Builder.Driver.FindElements(By.XPath(string.Format("//option[contains(text(), {0}]", elementTarget))).Click();
}
I've also tried to find the element by label via xpath similar to the above but it never finds it. What is the correct way to find the elements and check them?
When you want to click on each checkbox you can use :
var vehicleGroupInputElements = Builder.Driver.FindElements(By.ClassName("vehGrp"));
foreach (IWebElement checkbox in vehicleGroupInputElements)
{
checkbox.Click();
}
Just looked into Xpath syntax and found the answer. With the help of Chrome's 'copy Xpath' function in inspect mode, I found the path needed and successfully clicked the input element.
Example Xpath of the first input is as follows (notice the HTML for label[index] is 1 more than the way C# would count.)
//*[#id="ratingModification_SupplierContact_content"]/label[1]/input
And solution as follows
//Retrieves the checked items from the form and sends them to the page.
foreach (object checkedItem in vehicleGroupList.CheckedItems)
{
//Gets the index of the checked items.
int checkedIndex = vehicleGroupList.Items.IndexOf(checkedItem);
//Adds 1 to the index to match format of the page HTML.
checkedIndex++;
//Puts the index+1 into a string.
string indexText = checkedIndex.ToString();
//Finds the element by index+1.
var clickTarget = Builder.Driver.FindElement(By.XPath(string.Format("//*[#id='ratingModification_SupplierContact_content']/label[" +indexText+ "]/input")));
clickTarget.Click();
I am using C# selenium driver in visual studio for automating my scripts.The data in my radio button gets dynamically generated and I want to select the radio button using index . These are the ways I tried
Method 1
new SelectElement(driver.FindElement(By.Id("XX"))).SelectByIndex(2);Click();
In first method , I am not able to relate the Click to the element
Method 2
IWebElement element = driver.FindElement(By.("XX"));
System.Threading.Thread.Sleep(2000);
element.Click();
In method 2 , I am not sure how to pass the index.
This is my HTML code :
<input type="radio" name="XXXX" id="XXXX" value="5273786">.
So These radio buttons get dynamically generated. For eg, if I have 3 radio buttons, all 3 radio buttons have the same id and name but a different value.
So it would be great if you could let me know how to select the first radio button by passing its value or by selecting the radio button using index.
You should try as below :-
if you to select radio button by their value try as below using By.XPath() :-
IWebElement element = driver.FindElement(By.Xpath("//input[#value = '5273786']"));
element.Click();
Or try as below using By.Id() :-
IList<IWebElement> elements = driver.FindElements(By.TagName("select"));
var element = selectElements.Where(se => se.GetAttribute('value') == '5273786');
element.Click();
if you want to select radio button using index try as below using By.Xpath() :-
IWebElement element = driver.FindElement(By.Xpath("(//input[#id = 'XXXX'])[1]"));
element.Click();
Or try as below using By.Id() :-
IList<IWebElement> elements = driver.FindElements(By.Id("XXXX"));
elements[0].Click();
Hope it helps...:)
Have some problems trying to solve this. Have two DropDownListFor where the first is populated with data and the second should be populated using the selected value from the first.
Lets say the first DropDownlist contains these data:
RoutesList;
Value = "CoOs", Text = "Copenhagen - Oslo",
Value = "StOs", Text = "Stockholm - Oslo",
Value = "OsCo", Text = "Oslo - Copenhagen"
In my razor view I'm trying to create a new list based on the selected value.
How do I get the selected value, lets say "StOs" and format it so it only contains the first two letters "St"? Trying to do this with C# and not Jquery.
My code
// Loads RoutesList to departureRouteSelectList
var departureRouteSelectList = Model.RoutesList;
// Finds selected value
var selectedItem = departureRouteSelectList.Select(x => x.Value);
// Create new selectList for returnRoute
List<SelectListItem> returnRouteSelectList = null;
I'm not sure if the "Select" command does what I want and getting the "StOs"?
Understanding what you want make me think this post will resolve your problem:
Can't get my DropDownListFor to select a selected SelectListItem item in a Dropdown menu