how to print the elements with text value that contains in a list selenium c#
IList<IWebElement> attachmentList = driver.FindElements(By.ClassName("comment-box"));
foreach (IWebElement element in attachmentList)
{
System.Threading.Thread.Sleep(2000);
Console.WriteLine(element.Text);
}
it works fine by putting the thread.sleep code
How to get all elements into list or string with Selenium using C#?
You can get all of the element text like this:
IList<IWebElement> all = driver.FindElements(By.ClassName("comments"));
String[] allText = new String[all.Count];
int i = 0;
foreach (IWebElement element in all)
{
allText[i++] = element.Text;
}
Is there a way to find elements that have a text value less than a variable?
In this case, you could make your CSS selector a little lighter and change it to div.Price--amount
(note that the correct way to select by class
attribute is using .{class-name}
).
Next thing you probably want, if I understand your problem right, is to select multiple elements and not just the first one. You can achieve this by calling find_elements
instead of find_element
.
The last thing is the .text()
method returns a str
object and you need to compare it by its numerical (float
) value. You want to convert it first.
for element in driver.find_elements(By.CSS_SELECTOR, "div.Price--amount"):
try:
if float(element.text) < Snipeprice:
print("We've got a match")
except ValueError:
print(value, "is not a valid price")
After I send text to a text box using selenium how do i save that text as a variable(string) and then print that to console
Dylan - Take a look at Faker.net as a nuget package if you are looking for Random Data.
ex.
string rndFirstName = Faker.Name.First();
string rndMiddleName = Faker.Name.First();
string rndLastName = Faker.Name.Last();
then you can find the input box
driver.FindElement(By.Id("firstname").sendKeys(rndFirstName);
driver.FindElement(By.Id("middlename").sendKeys(rndMiddleName);
driver.FindElement(By.Id("lastname").sendKeys(rndLastName);
Then to see what the field value now has:
var newFirst = driver.FindElement(By.Id("firstname").GetAttribute("value");
Console.WriteLine("Name: "+ newFirst);
Related Topics
Copy Few Items from One List to Another List of Same Type in C#
Passing a List of Objects into an MVC Controller Method Using Jquery Ajax
How to Check for a Valid Base64 Encoded String
How to Get User Browser Name ( User-Agent ) in ASP.NET Core
How to Wait Until Task Is Finished in C#
How to Fix Json.Net (Newtonsoft.Json) Runtime File Load Exception
Pass Variable in Json String in C#
How to Get Group of Records With Latest Date Using Linq to Entities
How to Match Hyphens With Regular Expression
Extract First Element from Json
Calling a Function from Another Class in C#/.Net MVC App
How to Check If a Datetime Value Is Empty or Not in a Put Request
Client Specific Role Based Authentication
How to Secure the Asp.Net_Sessionid Cookie
Linq: Select an Object and Change Some Properties Without Creating a New Object