I wrote a code to list the names of the wordpress categories. But I was not successful. The category names column is marked in red.
https://i.stack.imgur.com/NnLk1.jpg
this is the code I wrote:
var table = driver.FindElement(By.TagName("tbody"));
var rows = table.FindElements(By.TagName("tr"));
foreach (var row in rows)
{
IList<IWebElement> satirlar = row.FindElements(By.TagName("td"));
foreach (var satir in satirlar)
{
var a = satir.Text;
listBox1.Items.Add(a);
}
}
output:
https://i.stack.imgur.com/zzWgb.jpg
I just want to list the category names. but the number of articles in the category and the short name of the category are listed. I don't want this to happen. Only category names should be listed. Can you help me please?
Try this:
var a=satir.FindElement(By.CssSelector("a.row-title")).Text;
Related
Well I have a list which is called employeeList. In the emoloyee Liste there are stored employees with some attributes like id, lastname ...
One of the attributes is Department. Now I want to create Radio Buttons with the Department. As there are more employees per department one department can appear several times but this I'd like to avoid. This is what I already got:
for (int i = 0; i < employeeList.Count(); i++)
{
panDepartments.Children.Add(new RadioButton() { Content = employeeList.ElementAt(i).Department.Distinct()});
}
Just filter the list before you iterate over it. One possibility would be LINQ (i.e. Distinct()) or just do the filtering yourself.
I think you can try something like this.
//First of all we need to get unique departments.
var departments = employeeList.Select(e => e.Department).Distinct();
//And after that we can create radio buttons and add them as children elements.
foreach(var dep in departments)
{
panDepartments.Children.Add(new RadioButton() { Content = dep });
}
//foreach loop can be replaced something like this if "panDepartments.Children"
//supports "AddRange" method
var radioBtns = departments.Select(d => new RadioButton() { Content = dep });
panDepartments.Children.AddRange(radioBtns);
I'm new with selenium, I can not figure out how to find some elements from an HTML page.
I reproduced the page here :
http://www.ladiana.it/test/test.htm
I need to click on this button :
Selection
and after the table appears, to read and write their values :
values
I'm using c# and I tried all possible way, I also tried to list all the elements with the Id
: "targetDemandReductionMW"
IList IWebElement> all = driver2.FindElements(By.Id("targetDemandReductionMW"));
I get a list of 52 void elements.
Here some other code , it doesn't generate any error but didn't work:
IWebElement element_xpath = driver2.FindElement(By.ClassName("tab_buttonSel"));
element_xpath.Click();
IWebElement table = driver2.FindElement(By.Id("drr1OfferAM"));
var rows = table.FindElements(By.TagName("tr"));
int conteggio = 0;
foreach (var row in rows)
{
var rowTds = row.FindElements(By.TagName("td"));
foreach (var td in rowTds)
{
IWebElement a = td.FindElement(By.XPath("//*[#id='targetDemandReductionMW']"));
Console.WriteLine(conteggio++.ToString()+" "+a.GetAttribute("href")+" "+ a.Text);
}
}
Any help?
Thanks
For the button you need to click, you can use:
//div[#id='tabContainer']/div[2]/following::tr/td[text()='DRR1 Offer']
For the xpath for the table records you can use:
//table[#id='drr1OfferPMHead']//td[#id='targetDemandReductionMW']
This returns the 13 rows that you are looking for. You can then do FindElements on this and get a list.
List<String> search = new List<string>();
IReadOnlyList<IWebElement> cells = driver.FindElements(By.Xpath("//table[#id='drr1OfferPMHead']//td[#id='targetDemandReductionMW']"));
foreach (IWebElement cell in cells)
{
search.Add(cell.Text);
}
The cell (in the html) has cellclick event , but if i try with :
foreach (IWebElement cell in cells)
{
search.Add(cell.Text);
if (cell.Text=="300.0")
{
cell.click();
}
}
it come out this error : "OpenQA.Selenium.ElementNotInteractableException: 'element not interactable"
I can able to read the table values BUT Im unable to add the values inside foreach loop and returning them as List<string>.
Please share your approach and Will be great helpful
IWebElement tableElement = driver.FindElement(By.XPath("//div[#id='table']"));
IList<IWebElement> tableRow = tableElement.FindElements(By.TagName("tr"));
IList<IWebElement> rowTD;
foreach (IWebElement row in tableRow)
{
rowTD = row.FindElements(By.TagName("td"));
//add the values in list??
}
return list
After this how can I add the td text values into an List<string>? I want this function to return the list.
I'm guessing that you are trying to return all of the table data elements as a list of strings.
If you want to stick with a similar approach of only getting the text of the td elements, then I would do something like this:
IWebElement tableElement = driver.FindElement(By.XPath("//div[#id='table']"));
IList<IWebElement> tableDataElements = tableElement.FindElements(By.TagName("td"));
var reults = new List<string>();
foreach (IWebElement tableDataElement in tableDataElements)
{
var tableData = tableDataElement.Text;
reults.add(tableData);
}
return reults;
You can combine Michiel's answer with LINQ to do this even more concisely--without a foreach loop at all:
First, add
using System.Linq;
Then, you can replace your entire code block with:
return driver.FindElement(By.XPath("//div[#id='table']"))
.FindElements(By.TagName("td"))
.Select(e => e.Text)
.ToList();
Here i upload one Notepad file in Datatable.it contains only phone numbers and doesnot contain any Headers.so i want to Remove the duplicates from My Datatable.it contains single column only.
When i do this i get the result but one value will be Duplicated.
9988775566
9988556644
9966332200
9988775566
like this one value will get again.i want to get the Datatable with out Duplicates.
My Snippet is
public void duplicatesinnotepad(DataTable dt, string col)
{
ArrayList unique = new ArrayList();
ArrayList duplicat = new ArrayList();
foreach (DataRow de in dt.Rows)
{
if (unique.Contains(de[col]))
duplicat.Add(de);
else
unique.Add(de[col]);
}
foreach (DataRow de in duplicat)
{
dt.Rows.Remove(de);
}
}
Try this
using Linq To Get Distinct Elements
var list = (
from row in dt.AsEnumerable()
select row.Field<string>("contacts")).Distinct();
OR
var list = dt.AsEnumerable().
GroupBy(item => item.Field<string>(dt.Columns["contacts"]));
I am using the following code in my .NET web service that gets its data form a CSV file.
private List<Item> ietms = new List<Item>();
public ItemRepository()
{
string filename = HttpRuntime.AppDomainAppPath + "App_Data\\items.csv";
var lines = File.ReadAllLines(filename).Skip(1).ToList();
for (int i = 0; i < lines.Count; i++)
{
var line = lines[i];
var columns = line.Split('$');
//get rid of newline characters in the middle of data lines
while (columns.Length < 9)
{
i += 1;
line = line.Replace("\n", " ") + lines[i];
columns = line.Split('$');
}
//Remove Starting and Trailing open quotes from fields
columns = columns.Select(c => { if (string.IsNullOrEmpty(c) == false) { return c.Substring(1, c.Length - 2); } return string.Empty; }).ToArray();
var temp = columns[5].Split('|', '>');
items.Add(new Item()
{
Id = int.Parse(columns[0]),
Name = columns[1],
Description = columns[2],
Category = temp[0]
});
}
}
This code gets a list of products from the CSV file along with its name, description etc. Each product belongs to either one or two categories : Category = temp[0].
Each product's category is found in a column of the csv file with it's data structured as such:
Groups>Subgroup>item, in which case, this product belongs to category "Groups".
The category column of a product may also be structured as:
MajorGroup|Groups>Subgroup>item, in which case this product belongs to category "MajorGroup".
Also, in many cases a product's category column may be structured as:
MajorGroup|Groups>Subgroup>item|SecondGroup, in which case this product belong to both the categories "MajorGroup" and "SecondGroup"
The code above that I am currently using does half the job. If a product has a category defined in the CSV file as MajorGroup|Groups>Subgroup>item|SecondGroup, it assigns it to category "MajorGroups" but not "SecondGroup".
This line var temp = columns[5].Split('|', '>'); gets the first value structured taht way and separated by a pipe and sets it as the product's category here Category = temp[0].
How do I fix this so that if the category is structured as MajorGroup|Groups>Subgroup>item|SecondGroup, with two categories, then it will show up in both categories.
How do I assign the product to one or more categories depending on the structure of the category column data.
This works for the most part, but how do I alter the code to check and assign for both categories?
Can I change this var temp = columns[5].Split('|', '>'); to get both teh first and the last value if it exists and assign both to Category = temp[0].
To get the second group values given the problem statement as specified you could do the following.
...
var temp = columns[5].Split('|', '>');
string categories= temp[0];
if (input.Count(x => x == '|') >= 2)
{
categories+= "," + temp.Last();
}
...
Category = categories;
Then one could get a list of Items that is assigned to a category by the following function:
static public IList<Item> GetProductsByCategory(string category, IList<Item> items)
{
return items.Where(x => x.Category.Split(',').Contains(category,StringComparer.OrdinalIgnoreCase)).ToList();
}
A much cleaner solution is to store the categories within the Item class as something that implements ILIST.
You should definitely use some CSV parser instead of doing this manually. There are too many potential problems and issues when parsing CSV manually that it is much easier and faster to use some existing tool like:
FileHelpers
Fast CSV Reader