Reputation: 464
I am trying to create a WebElement
object. I saw that there is a IWebElement
interface which i could implement.
I also saw this question and did't manage to implement it sucessfully.
In my usecase I get from a webpage all forms and then take the one with the most input
tags which haven't got the type
attribute set to hidden
.
this is my usecase code:
using System;
using System.Linq;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
namespace AutoWinner
{
class Program
{
static void Main(string[] args)
{
IWebDriver driver = new ChromeDriver();
driver.Url = "https://keepass.info/help/kb/testform.html";
var forms = driver.FindElements(By.TagName("form"));
var longestFormLengthOfAllForms = 0;
// I don't the p element.
// It's more here to just get a webElement which ic can later overwrite.
var mainForm = driver.FindElement(By.TagName("p"));
foreach (var form in forms)
{
Console.WriteLine(form.GetAttribute("outerHTML"));
var children = form.FindElements(By.TagName("input"));
var lengthOfCurrentForm = children.Count(x => x.GetAttribute("type") != "hidden");
if (lengthOfCurrentForm > longestFormLengthOfAllForms)
{
longestFormLengthOfAllForms = lengthOfCurrentForm;
mainForm = form;
}
}
Console.WriteLine(mainForm.GetAttribute("outerHTML"));
}
}
}
The line var mainForm = driver.FindElement(By.TagName("p"));
is meant to be a global variable where later save my main form in it. I don't need the p
element.
my Idea was to remove by creating a standard webElement
.
How could i get rid of it or improve it?
Upvotes: 0
Views: 5161
Reputation: 25714
Given your current usecase, I would suggest the code below. It grabs the FORM
tags, loops through each one and counts the INPUT
s that aren't type='hidden'
. Once it falls out of the for-each
, mainForm
is the FORM
with the largest number of desired INPUT
s.
IWebElement mainForm = null;
IReadOnlyCollection<IWebElement> forms = Driver.FindElements(By.CssSelector("form"));
int maxCount = 0;
foreach (IWebElement form in forms)
{
int count = form.FindElements(By.CssSelector("input:not([type='hidden'])")).Count;
if (count > maxCount)
{
maxCount = count;
mainForm = form;
}
}
// do something with mainForm
Console.WriteLine(mainForm?.GetAttribute("name"));
I tested this with the sample HTML page I created, below
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<table border="1">
<tr><td>
<form name="form1">
<p>Field1:<br><input type="text"></p>
<p>Field2:<br><input type="hidden"></p>
<p>Field3:<br><input type="hidden"></p>
<p>Field4:<br><input type="hidden"></p>
<p>Field5:<br><input type="text"></p>
<p><input type="submit" value="Submit">
</form>
</td></tr>
<tr><td>
<form name="form2">
<p>Field1:<br><input type="text"></p>
<p>Field2:<br><input type="hidden"></p>
<p>Field3:<br><input type="text"></p>
<p>Field4:<br><input type="hidden"></p>
<p>Field5:<br><input type="text"></p>
<p><input type="submit" value="Submit">
</form>
</td></tr>
<tr><td>
<form name="form3">
<p>Field1:<br><input type="text"></p>
<p>Field2:<br><input type="text"></p>
<p>Field3:<br><input type="text"></p>
<p>Field4:<br><input type="hidden"></p>
<p>Field5:<br><input type="text"></p>
<p><input type="submit" value="Submit">
</form>
</td></tr>
</table>
</body>
</html>
Upvotes: 3
Reputation: 29382
I still do not get your use case. But anyway if you are looking for something like this :
Let's say you have two web element like this :
var ele1 = driver.FindElement(By.Xpath("//"));
var ele2 = driver.FindElement(By.Xpath("//"));
ele1 is pointing to one element, whereas ele2 is pointing to second element.
ele2 = ele1
Now, ele2
should point to ele1
.
Not sure if you are looking for the same thing. Can't add this much as comment also.
Upvotes: 0