New projct BrowserDriver infered to help with webscraping
This commit is contained in:
248
BrowserHelper/Extensions/HtmlTableExtension.cs
Normal file
248
BrowserHelper/Extensions/HtmlTableExtension.cs
Normal file
@ -0,0 +1,248 @@
|
||||
using OpenQA.Selenium;
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace BrowserHelper.Extensions
|
||||
{
|
||||
public static class HtmlTableExtension
|
||||
{
|
||||
//Read of Table
|
||||
private static List<TableDataCollection> ReadTable(IWebElement table)
|
||||
{
|
||||
var tableDataCollection = new List<TableDataCollection>();
|
||||
var columns = table.FindElements(By.TagName("th"));
|
||||
var rows = table.FindElements(By.TagName("tr"));
|
||||
int rowIndex = 0;
|
||||
foreach (var row in rows)
|
||||
{
|
||||
int colIndex = 0;
|
||||
var colDatas = row.FindElements(By.TagName("td"));
|
||||
if (colDatas.Count != 0)
|
||||
foreach (var colValue in colDatas)
|
||||
{
|
||||
tableDataCollection.Add(new TableDataCollection
|
||||
{
|
||||
RowNumber = rowIndex,
|
||||
ColumnName = columns[colIndex].Text != "" ?
|
||||
columns[colIndex].Text : colIndex.ToString(),
|
||||
ColumnValue = colValue.Text,
|
||||
ColumnSpecialValue = GetControl(colValue)
|
||||
});
|
||||
colIndex++;
|
||||
}
|
||||
rowIndex++;
|
||||
}
|
||||
return tableDataCollection;
|
||||
}
|
||||
|
||||
private static List<TableDataCollection> ReadTablePartly(IWebElement table, string[] shares, string[] shareHeaders)
|
||||
{
|
||||
var tableDataCollection = new List<TableDataCollection>();
|
||||
var columns = table.FindElements(By.TagName("th"));
|
||||
var rows = table.FindElements(By.TagName("tr"));
|
||||
var antal = shares.Length;
|
||||
int rowIndex = 0;
|
||||
foreach (var row in rows)
|
||||
{
|
||||
int colIndex = 0;
|
||||
var colDatas = row.FindElements(By.TagName("td"));
|
||||
if (colDatas.Count != 0 && shares.Contains(colDatas[0].Text))
|
||||
{
|
||||
if (colDatas.Count != 0)
|
||||
foreach (var colValue in colDatas)
|
||||
{
|
||||
if (shareHeaders.Contains(columns[colIndex].Text))
|
||||
{
|
||||
tableDataCollection.Add(new TableDataCollection
|
||||
{
|
||||
RowNumber = rowIndex,
|
||||
ColumnName = columns[colIndex].Text != "" ?
|
||||
columns[colIndex].Text : colIndex.ToString(),
|
||||
ColumnValue = colValue.Text,
|
||||
ColumnSpecialValue = GetControl(colValue)
|
||||
});
|
||||
}
|
||||
colIndex++;
|
||||
}
|
||||
rowIndex++;
|
||||
antal--;
|
||||
}
|
||||
if (antal < 1) break;
|
||||
}
|
||||
return tableDataCollection;
|
||||
}
|
||||
|
||||
|
||||
private static ColumnSpecialValue GetControl(IWebElement columnValue)
|
||||
{
|
||||
ColumnSpecialValue? columnSpecialValue = null;
|
||||
if (columnValue.FindElements(By.TagName("a")).Count > 0)
|
||||
{
|
||||
columnSpecialValue = new ColumnSpecialValue
|
||||
{
|
||||
ElementCollection = columnValue.FindElements(By.TagName("a")),
|
||||
ControlType = ControlType.hyperlink
|
||||
};
|
||||
}
|
||||
if (columnValue.FindElements(By.TagName("input")).Count > 0)
|
||||
{
|
||||
columnSpecialValue = new ColumnSpecialValue
|
||||
{
|
||||
ElementCollection = columnValue.FindElements(By.TagName("input")),
|
||||
ControlType = ControlType.input
|
||||
};
|
||||
}
|
||||
return columnSpecialValue;
|
||||
}
|
||||
|
||||
public static List<TableDataCollection> ReadHandledStock(this IWebElement table)
|
||||
{
|
||||
var tableDataCollection = new List<TableDataCollection>();
|
||||
var columns = table.FindElements(By.TagName("th"));
|
||||
var rows = table.FindElements(By.TagName("tr"));
|
||||
var antal = rows.Count;
|
||||
int rowIndex = 0;
|
||||
foreach (var row in rows)
|
||||
{
|
||||
var colDatas = row.FindElements(By.TagName("td"));
|
||||
if (colDatas.Count != 0)
|
||||
{
|
||||
if (colDatas.Count != 0)
|
||||
tableDataCollection.Add(new TableDataCollection
|
||||
{
|
||||
RowNumber = rowIndex,
|
||||
ColumnName = columns[0].Text,
|
||||
ColumnValue = colDatas[0].Text,
|
||||
ColumnSpecialValue = null
|
||||
});
|
||||
}
|
||||
rowIndex++;
|
||||
antal--;
|
||||
if (antal < 0) break;
|
||||
}
|
||||
|
||||
return tableDataCollection;
|
||||
}
|
||||
|
||||
|
||||
public static List<Share> GetCertainStocks(this IWebElement element, string[] shares, string[] shareHeaders)
|
||||
{
|
||||
List<Share> shareList = new List<Share>();
|
||||
var reducedList = ReadTablePartly(element, shares, shareHeaders);
|
||||
int rNr = -1;
|
||||
Share Aktie = null;
|
||||
foreach (var shareRow in reducedList)
|
||||
{
|
||||
if (shareRow.RowNumber != rNr)
|
||||
{
|
||||
if (Aktie != null)
|
||||
{
|
||||
shareList.Add(Aktie);
|
||||
}
|
||||
Aktie = new();
|
||||
rNr = shareRow.RowNumber;
|
||||
}
|
||||
switch (shareRow.ColumnName)
|
||||
{
|
||||
case "Aktie":
|
||||
{
|
||||
Aktie.AktieNamn = shareRow.ColumnValue;
|
||||
break;
|
||||
}
|
||||
case "Senast":
|
||||
{
|
||||
Aktie.SenastePris = Convert.ToDecimal(shareRow.ColumnValue);
|
||||
break;
|
||||
}
|
||||
case "Tid":
|
||||
{
|
||||
Aktie.RegTime = TimeOnly.Parse(shareRow.ColumnValue);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
shareList.Add(Aktie);
|
||||
return shareList;
|
||||
}
|
||||
|
||||
|
||||
public static void PerformActionOnCell(this IWebElement element, string targetColumnIndex, string refColumnName, string refColumnValue, string controlToOperate = null)
|
||||
{
|
||||
var table = ReadTable(element);
|
||||
|
||||
foreach (int rowNumber in GetDynamicRowNumber(table, refColumnName, refColumnValue))
|
||||
{
|
||||
var cell = (from e in table
|
||||
where e.ColumnName == targetColumnIndex && e.RowNumber == rowNumber
|
||||
select e.ColumnSpecialValue).SingleOrDefault();
|
||||
|
||||
if (controlToOperate != null && cell != null)
|
||||
{
|
||||
IWebElement elementToClick = null;
|
||||
|
||||
if (cell.ControlType == ControlType.hyperlink)
|
||||
{
|
||||
elementToClick = (from c in cell.ElementCollection
|
||||
where c.Text == controlToOperate.ToString()
|
||||
select c).SingleOrDefault();
|
||||
}
|
||||
if (cell.ControlType == ControlType.input)
|
||||
{
|
||||
elementToClick = (from c in cell.ElementCollection
|
||||
where c.GetAttribute("value") == controlToOperate.ToString()
|
||||
select c).SingleOrDefault();
|
||||
|
||||
}
|
||||
elementToClick?.Click();
|
||||
}
|
||||
else
|
||||
{
|
||||
cell.ElementCollection?.First().Click();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private static IEnumerable GetDynamicRowNumber(List<TableDataCollection> tableCollection, string columnName, string columnValue)
|
||||
{
|
||||
foreach (var table in tableCollection)
|
||||
{
|
||||
if (table.ColumnName == columnName && table.ColumnValue == columnValue)
|
||||
yield return table.RowNumber;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class TableDataCollection
|
||||
{
|
||||
public int RowNumber { get; set; }
|
||||
public string? ColumnName { get; set; }
|
||||
public string? ColumnValue { get; set; }
|
||||
public ColumnSpecialValue? ColumnSpecialValue { get; set; }
|
||||
}
|
||||
|
||||
public class ColumnSpecialValue
|
||||
{
|
||||
public IEnumerable<IWebElement>? ElementCollection { get; set; }
|
||||
public ControlType? ControlType { get; set; }
|
||||
}
|
||||
|
||||
public enum ControlType
|
||||
{
|
||||
hyperlink,
|
||||
input,
|
||||
option,
|
||||
select
|
||||
}
|
||||
|
||||
public class Share
|
||||
{
|
||||
public string AktieNamn { get; set; }
|
||||
public decimal SenastePris { get; set; }
|
||||
public TimeOnly RegTime { get; set; }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user