I have 10 <iframe> elements in my aspx page and they are named f1,f2,.......,f10. What I want to do is to put theme in a list/array so that I can reference them by index.
Please help me with this.
var framearray = document.getElementsByTagName("iframe");
for (var i = 0; i < framearray.length; i++)
{
var aframe = framearray[i];
//do stuff
}
Each control (your Page being one of them) has a Controls property that get a list of all sub-controls.
this.Controls; //will list all sub controls on the page
You can search through that for your items.
Alternately, you can use the FindControl() function on a Control object to get a control based on its ID.
var cnt = this.FindControl("f1");
Copy this line 10 times or make it in a loop, and make them add to an array:
List<Controls> iframeList = new List<Controls>();
for(int i = 1; i <= 10; i++)
iframeList.Add(this.FindControl("f" + i);
<iframe> elements are automatically indexed by the browser in a list object called window.frames, you can iterate this array-like object with a standard for() loop --> https://developer.mozilla.org/en-US/docs/Web/API/window.frames
Related
im using IList to get a list of elements and the text value to be able to click on the correct element
IList<IWebElement> PONumbers = driver.FindElements(By.ClassName("first-column"));
int POCount = PONumbers.Count;
for (int i = 0; i < POCount; i++)
{
String PONo = PONumbers.ElementAt(i).Text;
if (PONo.Equals(PONoNonBatchNonTonne))
{
PONumbers.ElementAt(i).Click();
break;
}
}
It gets all the correct elements which is 36
It only gets the text for the first 8 elements and then the rest are showing as ""
this is the html code:
it seems to be only getting the text of the elements in the view, but then why would it get all elements
any help welcome?
Since the element is hidden on webpage you have to use textContent attribute to get the value. To click on the element either scroll the page or use javaScript executor to click on the specific element.
IList<IWebElement> PONumbers = driver.FindElements(By.ClassName("first-column"));
int POCount = PONumbers.Count;
for (int i = 0; i < POCount; i++)
{
String PONo = PONumbers.ElementAt(i).GetAttribute("textContent");
if (PONo.Equals(PONoNonBatchNonTonne))
{
IJavaScriptExecutor executor = (IJavaScriptExecutor)driver;
executor.ExecuteScript("arguments[0].click();", PONumbers.ElementAt(i));
break;
}
}
Selenium only gets the elements that are in the view.
Once you have the last element you should be able to select it, send it some keys another 8 times, arrow down usually,and get the next 8 elements.
This would be have to be done for each set of 8 until you get the last one.
The web is loading content dynamically through jsp or similar and selenium needs to manually access all of the content that is dynamically loaded.
In order to test my theory run your test and go back to the browser window, select the last element in the view and see which key selects the next element and moves the elements in the view.
To clarify my question more I'll first show you add a checkbox using the blazor component library I'm using:
<MudCheckBox #bind-Checked="#Basic_CheckBox1"></MudCheckBox>
#code{
public bool Basic_CheckBox1 { get; set; } = false;
}
the code above creates a checkbox and then toggles the boolean Basic_CheckBox1 to true or false based on if the checkbox is checked or not.
here is my code
#for (int i = 0; i < checkboxNames.Count; i++)
{
<MudCheckBox #bind-Checked="#checkboxBools[i]" Label="#checkboxNames[i]"></MudCheckBox>
}
this code works until I check or uncheck my created checkboxes, then I get the index out of range error, how can I fix this? I need to use a list of booleans because I don't know how many checkboxes I will need until runtime, if there is any other solution please let me know.
Think of it like the bind isn't done there and then in the loop, but later. By the time the bind comes to be done, the value of i is off the end of the array Length, having been incremented repeatedly. Instead, you can save the current value of your loop variable i into another variable, then use that other variable in your bind:
#for (int i = 0; i < checkboxNames.Length; i++)
{
var x = i;
<MudCheckBox #bind-Checked="#checkboxBools[x]" Label="#checkboxNames[i]"></MudCheckBox>
}
checkBoxBools should be an array/list that is the same length (initalized to have the same number of elements as) checkBoxNames.. For example:
string[] checkboxNames = new string[]{"a","b","c"};
bool[] checkboxBools = new bool[3];
Or, if something else is providing a variable number of names:
checkboxNames = context.People.Select(p => p.Name).ToArray();
checkboxBools = new bool[checkBoxNames.Length];
https://try.mudblazor.com/snippet/GamQEFuFIwdRbzEx
I am trying to read the following list:
<ol class="sublist">
<li>
Sort Out Your Values
</li>
<li>
Establish Realistic Goals
</li>
<li>
Determine Your Monthly Net Income
This is the code i wrote for it; but currently, everytime it runs; my string is coming up empty. I want to get the inner text so that in my loop i grab it and click it and return back to previous screen.
IWebElement container = driver.FindElement(By.ClassName("sublist"));
IList<IWebElement> elements = driver.FindElements(By.TagName("a"));
string [] newlink = new string[elements.Count()];
for (int i = 0; i < newlink.Count(); i++)
{
if (newlink[i] != null)
{
driver.FindElement(By.LinkText(newlink[i])).Click();
driver.WaitForElement(By.CssSelector("[id$='hlnkPrint']"));
driver.Navigate().Back();
}
}
The script is able to run but was getting that the links were null, so i added a check to see if any of them were null and it turns out all of them are.
Im sure it has something to do with with the '.text' or 'ToString', but Im not sure where to implement that.
Thanks
There's a few issues with your code.
- You haven't set the value of newlink, just created it.
- Count is a property, but you're using it as a method.
- Link text is the .Text property of an IWebElement, and you would need to access that.
- Your current code will likely click one link, and after going back will throw a StaleElementException.
In the following
- I set newlink to the Text values of the links found for elements
- I then iterate through the array of link text
IWebElement container = driver.FindElement(By.ClassName("sublist"));
IList<IWebElement> elements = container.FindElements(By.TagName("a"));
string[] newlink = new string[elements.Count];
for (int i = 0; i < newlink.Count; i++)
{
newlink[i] = elements[i].Text;
}
for (int i = 0; i < newlink.Count; i++)
{
if (newlink[i] != null)
{
driver.FindElement(By.LinkText(newlink[i])).Click();
driver.WaitForElement(By.CssSelector("[id$='hlnkPrint']"));
driver.Navigate().Back();
}
}
You can use FindElement() on an IWebElement. so in this case, if you want to find elements that are children of container, you would use container.FindElements().
I have a group of textbox controls that i would like to populate with an array of doubles.The controls names are numerically incremented like so
Tol1.Text = lineTolFront[0].ToString();
Tol2.Text = lineTolFront[1].ToString();
Tol3.Text = lineTolFront[2].ToString();
Tol4.Text = lineTolFront[3].ToString();
Tol5.Text = lineTolFront[4].ToString();
Tol6.Text = lineTolFront[5].ToString();
//and so on
is there a simpler way to do this using a loop without having to manually input the values?
First, get all those TextBoxes using LINQ (note: this is useful particularly when you have many controls you do not want manually put in a collection).
var tboxes = this.Controls.Cast<Control>()
.OfType<TextBox>()
.Where(l => l.Name.Contains("Tol"));
and then loop through them and set the content.
int i = 0;
foreach(var tb in tboxes)
tb.Text = lineTolFront[i++].ToString();
You could add the TextBoxes to a collection first. It's a little less copy/paste work at least.
var textBoxes = new List<TextBox> { Tol1, Tol2, Tol3, Tol4, Tol5, Tol6 };
for (var i = 0; i < lineTolFront.Count; i++)
textBoxes[i].Text = lineTolFront[i].ToString();
Regarding M Patel's comment, make sure you add the TextBoxes to the collection in the same order you want to assign the doubles from the lineTolFront array.
You could add your controls to an array and loop through it:
var controls = new[] { Tol1, Tol2, Tol3, Tol4, Tol5, }; //etc
for(int i = 0; i < controls.Length; i++)
{
controls[i].Text = lineTolFront[i].ToString();
}
I have about 20 checkboxes on the form. How can I name them to use them later in a for loop for example? A thing such an array checkBox[i].
Regards
I am assuming that the controls are being created as part of InitializeComponent(), i.e. it's done by designer code.
The straightforward approach would be to do this after InitializeComponent is called:
var checkboxes = new[]
{
checkBox1, // these are the names you have given
checkBox2, // to the checkboxes in the designer
checkBox3,
};
A better way would be to use LINQ to put all checkboxes in an array:
var checkboxes = this.Controls.OfType<CheckBox>().ToArray();
However, this will not work recursively and you may have to filter some checkboxes out of the collection if you don't want all of them to be in the array.
Info here: http://msdn.microsoft.com/en-us/library/aa289500(v=vs.71).aspx
Check out container controls, they automatically create collections of the controls you place within them in design view.
CheckBox[] MyCheckBoxes = new CheckBox[20];
for (int i = 0; i < 20; i++)
{
MyCheckBoxes[i] = new CheckBox();
MyCheckBoxes[i].Checked = true;
//etc
}