Click here to Skip to main content
15,886,678 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello


C#
IList<IWebElement> labels = driver.FindElements(By.ClassName(textBox1.Text));
            textBox2.Text = labels.ToString();
            listBox1.Items.Add(labels);


as the output of this code
in the Listbox1
(Collection)

in the textbox2
System.Collections.ObjectModel.ReadOnlyCollection`1[OpenQA.Selenium.IWebElement]


I'm getting these results
I can't get data from the site

What I have tried:

String labels = driver.FindElement(By.ClassName(textBox1.Text)).Text;
listBox1.Items.Add(labels);


one by one data craw, I get correct results, but not as a list
Posted
Updated 23-Feb-21 22:23pm

1 solution

The variable labels is an IList (Collection) of items. So calling labels.ToString() will correctly tell you that is is a Collection object. If you want to view the individual items then you need to iterate the collection with some type of for/foreach loop.
 
Share this answer
 
Comments
CL4Y3R-TR 24-Feb-21 4:48am    
I tried to add it to the list but it didn't work
listBox1.Items.Add(labels);
Richard MacCutchan 24-Feb-21 5:03am    
You cannot add a collection of items in that way. As I already explained you need to use a for/foreach loop to iterate the collection. Alternatively you could use a BindingSource. If you do not understand how classes and collections work then check the documentation for the classes you are using.
CL4Y3R-TR 24-Feb-21 5:13am    
IList<IWebElement> all = driver.FindElements(By.ClassName(textBox1.Text));

            String[] allText = new String[all.Count];
            int i = 0;
            foreach (IWebElement element in all)
            {
                allText[i++] = element.Text;
                listBox1.Items.Add(element);
            }


i tried this not worked
Richard MacCutchan 24-Feb-21 5:21am    
What is the purpose of the string array when you do not keep it, and you are not passing the element.Text property to the ListBox.Items? All you need to do is:
foreach (IWebElement element in all)
{
    listBox1.Items.Add(element.Text);
}

And if it still does not show any results, then you need to step through the code with your debugger to see what is in the list.
CL4Y3R-TR 24-Feb-21 5:29am    
Thank u Soo much

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900