336 lines
13 KiB
C#
336 lines
13 KiB
C#
using BrowserHelper.Settings;
|
|
using DataDomain;
|
|
using OpenQA.Selenium;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
|
|
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++;
|
|
}
|
|
antal--;
|
|
}
|
|
|
|
rowIndex++;
|
|
if (antal < 1) break;
|
|
}
|
|
return tableDataCollection;
|
|
}
|
|
|
|
public static List<DiTraderStockRow> SimpleReadTablePartlyOld(this IWebElement table, string[] shares, string[] shareHeaders)
|
|
{
|
|
var shareCollection = new List<DiTraderStockRow>();
|
|
var columns = table.FindElements(By.TagName("th"));
|
|
IWebElement tblBody = table.FindElement(By.XPath(".//tbody"));
|
|
var rows = tblBody.FindElements(By.TagName("tr"));
|
|
Dictionary<string, int> columnPos = new Dictionary<string, int>();
|
|
//GlobalStopwatch.PrintSecs("After allocations");
|
|
for (int i = 0; i < columns.Count; i++)
|
|
{
|
|
if (shareHeaders.Contains(columns[i].Text)) columnPos.Add(columns[i].Text, i);
|
|
}
|
|
var antal = shares.Length;
|
|
int rowIndex = 0;
|
|
//GlobalStopwatch.PrintSecs("Before scraplooping");
|
|
foreach (var row in rows)
|
|
{
|
|
var element = row.FindElement(By.XPath(".//td"));
|
|
if (!shares.Contains(element.Text)) { }
|
|
else
|
|
{
|
|
var colDatas = row.FindElements(By.TagName("td"));
|
|
if (colDatas.Count != 0 && shares.Contains(colDatas[0].Text))
|
|
{
|
|
shareCollection.Add(new DiTraderStockRow
|
|
{
|
|
StockName = colDatas[columnPos["Aktie"]].Text,
|
|
LatestPrice = decimal.Parse(colDatas[columnPos["Senast"]].Text),
|
|
TimeOfDay = TimeSpan.Parse(colDatas[columnPos["Tid"]].Text)
|
|
});
|
|
|
|
rowIndex++;
|
|
antal--;
|
|
}
|
|
}
|
|
if (antal < 1) break;
|
|
}
|
|
//GlobalStopwatch.PrintSecs("After scraplooping");
|
|
return shareCollection;
|
|
}
|
|
|
|
|
|
public static List<DiTraderStockRow> SimpleReadTablePartly(this IWebElement table, string[] shares, string[] shareHeaders)
|
|
{
|
|
var shareSet = new HashSet<string>(shares);
|
|
|
|
var shareCollection = new List<DiTraderStockRow>();
|
|
var columns = table.FindElements(By.TagName("th"));
|
|
IWebElement tblBody = table.FindElement(By.XPath(".//tbody"));
|
|
var rows = tblBody.FindElements(By.TagName("tr"));
|
|
Dictionary<string, int> columnPos = new Dictionary<string, int>();
|
|
//GlobalStopwatch.PrintSecs("After allocations");
|
|
for (int i = 0; i < columns.Count; i++)
|
|
{
|
|
if (shareHeaders.Contains(columns[i].Text)) columnPos.Add(columns[i].Text, i);
|
|
}
|
|
//var antal = shares.Length;
|
|
int rowIndex = 0;
|
|
//GlobalStopwatch.PrintSecs("Before scraplooping");
|
|
foreach (var row in rows)
|
|
{
|
|
var element = row.FindElement(By.XPath(".//td"));
|
|
if (!shareSet.Contains(element.Text)) { }
|
|
else
|
|
{
|
|
var colDatas = row.FindElements(By.TagName("td"));
|
|
if (colDatas.Count != 0 && shares.Contains(colDatas[0].Text))
|
|
{
|
|
shareCollection.Add(new DiTraderStockRow
|
|
{
|
|
StockName = colDatas[columnPos["Aktie"]].Text,
|
|
LatestPrice = decimal.Parse(colDatas[columnPos["Senast"]].Text),
|
|
TimeOfDay = TimeSpan.Parse(colDatas[columnPos["Tid"]].Text)
|
|
});
|
|
|
|
rowIndex++;
|
|
//antal--;
|
|
shareSet.Remove(element.Text);
|
|
}
|
|
}
|
|
//if (antal < 1) break;
|
|
if (shareSet.Count < 1) break;
|
|
}
|
|
//GlobalStopwatch.PrintSecs("After scraplooping");
|
|
return shareCollection;
|
|
}
|
|
|
|
|
|
|
|
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<DiTraderStockRow> GetCertainStocks(this IWebElement element, string[] shares, string[] shareHeaders)
|
|
//{
|
|
// List<DiTraderStockRow> shareList = new List<DiTraderStockRow>();
|
|
// var reducedList = ReadTablePartly(element, shares, shareHeaders);
|
|
// int rNr = -1;
|
|
// DiTraderStockRow 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.StockName = shareRow.ColumnValue;
|
|
// break;
|
|
// }
|
|
// case "Senast":
|
|
// {
|
|
// Aktie.LatestPrice = Convert.ToDecimal(shareRow.ColumnValue);
|
|
// break;
|
|
// }
|
|
// case "Tid":
|
|
// {
|
|
// Aktie.TimeOfDay = TimeSpan.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; }
|
|
}
|
|
}
|