I'm having some trouble using Object Identification on a drop down list, what is being returned is this:
IListBox pricerBox= browser.Describe<IListBox>(new
ListBoxDescription
{
TagName = #"SELECT",
Name = #"select"
});
I would like to select a value from the drop down list and I tried using this as an example: How to select an option from drop down using Selenium WebDriver C#? But didn't have any luck as I'm using LeanFT in C# MS VS.
When inspecting in Chrome, this is the list box element details:
<select tabindex="1" style="width: 150px;">
<option value="3">Mc1</option><option value="11">Mc2</option><option
value="17">Mc3</option><option value="21">Mc4</option><option
value="57">Mc5</option></select>
This is what I coded so far:
//pricer box
IListBox pricerBox= browser.Describe<IListBox>(new
ListBoxDescription
{
TagName = #"SELECT",
Name = #"select"
});
Thread.Sleep(3000);
pricerBox.Click();
Please have a look at LeanFT's documentation for IListBox, the Select function looks like what you're looking for.
pricerBox.Select("Mc3"); // No need to sleep before this
Related
I have a form that uses Selenium to go to a page and automate tasks for a user. The only part of the page that changes is a CheckBoxList, and I've been trying to extract the labels from it and mirror them to my form's CheckedListBox so users can make the selection there without seeing the page.
So far I have this:
IList<IWebElement> vehicleGroups = Builder.Driver.FindElements(By.ClassName("vehGrp"));
String[] vehicleText = new String[vehicleGroups.Count];
int i = 0;
foreach (IWebElement element in vehicleGroups)
{
vehicleText[i++] = element.Text;
vehicleGroupList.Items.Add(element.Text);
}
Which works as far as getting the correct number of elements and populating the form, but all of the labels in vehicleText are blank (or just a space.)
An example of the HTML for one of the labels is
<label><input type="checkbox" name="searchQuery.vehicleGroups[0].isSelected" value="on" class="vehGrp"> abcd/efgh ijkl mn (opqrst)</label>
Did I miss something or is the " " messing with the label text? The "abcd/efgh ijkl mn (opqrst)" is what I need but it and the potential number of elements can change daily.
vehicleGroups are the <input> elements, not the <label>s that surround them - and these have no text. This is expected behavior.
You need to get the surrounding <label> element, for example using a method like this:
https://stackoverflow.com/a/12194481/7866667
var vehicleGroupInputElements = driver.FindElements(By.ClassName("vehGrp"));
var vehicleGroupNames = vehicleGroupInputElements
.Select(e => e.FindElement(By.XPath("..")))
.Select(e => e.Text)
.ToArray();
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'm using Archetype property editor and trying to grab only the first fieldset using foreach:
foreach (var fieldset in Umbraco.Content(5369).GetPropertyValue<Archetype.Models.ArchetypeModel>("myProperty"))
{
var icon = Umbraco.TypedMedia(fieldset .GetValue("icon"));
<img src="#icon.Url" />
<span>#fieldset.GetValue("iconTitle")</span>
}
Fieldset is of type {Archetype.Models.ArchetypeFieldsetModel}.
How can I get only the first fieldset ?
EDIT Tried:
var fieldset = Umbraco.Content(5369).GetPropertyValue<Archetype.Models.ArchetypeModel>("hotelFacilitesIcons").Fieldsets;
This gives me Archetype.Models.ArchetypeModel.Fieldsets' is inaccessible due to its protection level
Tried:
var fieldset = Umbraco.Content(5369).GetPropertyValue<Archetype.Models.ArchetypeModel>("myPropertyAlias");
var facility = fieldset.GetPropertyValue<List<Archetype.Models.ArchetypeFieldsetModel>>("myFieldSetAlias");
This gives me: 'Archetype.Models.ArchetypeModel' does not contain a definition for 'GetPropertyValue' from second line
I'll have a stab at perhaps trying to point you in the right direction. Looking at a previous project I have been involved in there is a Archetype.Models.ArchetypeFieldsetModel type so perhaps your content property might return a collection of Archetype.Models.ArchetypeFieldsetModel instead of Archetype.Models.ArchetypeModel?
E.g.
var fieldset = Umbraco.Content(5369).GetPropertyValue<List<Archetype.Models.ArchetypeFieldsetModel>>("myProperty").FirstOrDefault();
var icon = Umbraco.TypedMedia(fieldset .GetValue("icon"));
<img src="#icon.Url" />
<span>#fieldset.GetValue("iconTitle")</span>
I have sort of done the same thing in my case wanted to make sure the first archetype has got its first value set before getting all the others so how i got around it was by creating a var with the archetypes (which will give you a list fo one or more) then get the first one and check its value like below;
var contentSectionArchtype = Model.Content.GetPropertyValue<ArchetypeModel>("myArchetypeAlias");
var firstSection = contentSectionArchtype.First();
then the first section was always the first archetype which i would then check its first value before looping through all of them, hope that helps.
As I am new to MVC framework, I have been spending a couple of hours to generate a check box list in a View file. Finally, I could figure it out well. Here is my code:
#foreach (var item in Model.GetRoleNames)
{
#Html.CheckBox("chk_" + item.Value, new { value = item.Value })#item.Text<br />
}
But, I need to detect which of them is selected and if all the ckeckboxes are left unchecked, then preventing some operations.
Question: How can I get the checked items when I am within a controller action?
As the others said, you should use a Boolean value as the second parameter to CheckBox to indicate the checked status. A bit of string manipulation should help you get the ids of the selected check boxes..
First lets change the way the checkbox helper is used
<div id='myCheckboxDiv'>
#foreach (var item in Model.GetRoleNames)
{
#Html.CheckBox("chk_" + item.Value, false)#item.Text<br />
}
</div>
As you can see, I have set the second parameter to false and wrapped the mix in a div
And, when you want to get the ‘values’ associated with the selected checkboxes
var values = $(‘# myCheckboxDiv’).find('input:checkbox:checked').map(function () {
// get the name ..
var nameOfSelectedItem = this.attr('name');
// skip the ‘chk_’ part and give me the rest
return nameOfSelectedItem.substr(4);
}).get();
I am assuming item.Value to be a number. If its is not, please remove the white spaces using C#
I'm trying to automate in a WinForm using a WebBrowser control to navigate and pull report info from a website. You can enter values in textboxes and invoke the click events for buttons and links, but I have not figured out how select a option drop-down .... in a automated way. Anybody recommend how to select a item from a drop-down, given this html example:
<SELECT id="term_id" size="1" name="p_term_in"><option value="">Select Another Term<option value="201050">Summer 2010<option value="201010">Spring 2010<option value="200980">Fall 2009</SELECT>
For others that can learn from entering values to textboxes and invoking click events here's how you do it:
webBrowser1.Document.GetElementById("<HTML ELEMENT NAME>").SetAttribute("value", "THE NAME");
Invoke button or hyperlink click:
webBrowser1.Document.GetElementById("<BUTTON>").InvokeMember("click");
So I've solved entering values and invoking click, but I have not solved selecting a drop-down value.
Assuming you have the following select in the HTML:
<select id="term_id" size="1" name="p_term_in">
<option value="">Select Another Term
<option value="201050">Summer 2010
<option value="201010">Spring 2010
<option value="200980">Fall 2009
</select>
This should allow you to preselect the third value:
webBrowser1.Document.GetElementById("term_id").SetAttribute("value", "201010");
var select = webBrowser.Document.GetElementById("ddlProyectos");
mshtml.HTMLSelectElement cbProyectos = select.DomElement as mshtml.HTMLSelectElement;
var total = cbProyectos.length;
for (var i= 0; i < total; i++)
{
cbProyectos.selectedIndex = i;
if (cbProyectos.value.Contains("13963"))
{
break;
}
}
//cbProyectos.selectedIndex = 4;
select.InvokeMember("onchange");
select.Children[4].SetAttribute("selected", "selected");
var theElementCollection = webBrowser.Document.GetElementsByTagName("select");
foreach (HtmlElement el in theElementCollection)
{
if (el.GetAttribute("value").Equals("13963"))
{
el.SetAttribute("selected", "selected");
//el.InvokeMember("click");
}
}
You will have to select the selected attribute on the option you want.
Given:
<select id="mySelect">
<option>1</option>
<option>2</option>
<option>3</option>
</select>
The following would selct the third option:
webBrowser1.Document
.GetElementById("")
.Children.GetElementsByName("option")[2]
.SetAttribute("selected", "selected");
try this:
add reference to microsoft.mshtml in project --> add reference...
Dim cboTemp As mshtml.HTMLSelectElement
cboTemp = WebBrowser1.Document.GetElementById("myselect").DomElement
cbotemp.selectedindex = 2
having the variable cbotemp set to a select element gives you greater access to the control :)
HtmlElement hField = webBrowser1.Document.GetElementById("ID");
hField.SetAttribute("selectedIndex", "2");
select by index (zero based) not the value....
I'm answering on this post after five years, for the people who are searching a solution of this problem.
If you just need to submit/post a value for the dropdown then this line is sufficient:
webBrowser1.Document.GetElementById("term_id").SetAttribute("value", "200980");
But if you really need to select an underlying OPTION, then:
HtmlElement selectDom = webBrowser1.Document.GetElementById("term_id");
foreach (HtmlElement option in selectDom.GetElementsByTagName("option"))
{
if (option.GetAttribute("value") == "200980")
{
var dom = option.DomElement as dynamic;
dom.selected = true;
// selectDom.InvokeMember("onChange"); // if you need this too
break;
}
}
You can use this:
webBrowser1.Document.GetElementById("term_id").SetAttribute("value",yourText);