Skip to content

Commit bee61fe

Browse files
committed
Add project files.
1 parent 6983652 commit bee61fe

7 files changed

Lines changed: 235 additions & 0 deletions

File tree

Selenium.WebDriver.Linq.sln

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.5.33516.290
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Selenium.WebDriver.Linq", "Selenium.WebDriver.Linq\Selenium.WebDriver.Linq.csproj", "{1FC487B0-B6EF-4AB4-95F5-E2F6B863E91E}"
7+
EndProject
8+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestConsole", "TestConsole\TestConsole.csproj", "{3CD058F7-39A7-454D-AB12-F631802799E1}"
9+
EndProject
10+
Global
11+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
12+
Debug|Any CPU = Debug|Any CPU
13+
Release|Any CPU = Release|Any CPU
14+
EndGlobalSection
15+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
16+
{1FC487B0-B6EF-4AB4-95F5-E2F6B863E91E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
17+
{1FC487B0-B6EF-4AB4-95F5-E2F6B863E91E}.Debug|Any CPU.Build.0 = Debug|Any CPU
18+
{1FC487B0-B6EF-4AB4-95F5-E2F6B863E91E}.Release|Any CPU.ActiveCfg = Release|Any CPU
19+
{1FC487B0-B6EF-4AB4-95F5-E2F6B863E91E}.Release|Any CPU.Build.0 = Release|Any CPU
20+
{3CD058F7-39A7-454D-AB12-F631802799E1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
21+
{3CD058F7-39A7-454D-AB12-F631802799E1}.Debug|Any CPU.Build.0 = Debug|Any CPU
22+
{3CD058F7-39A7-454D-AB12-F631802799E1}.Release|Any CPU.ActiveCfg = Release|Any CPU
23+
{3CD058F7-39A7-454D-AB12-F631802799E1}.Release|Any CPU.Build.0 = Release|Any CPU
24+
EndGlobalSection
25+
GlobalSection(SolutionProperties) = preSolution
26+
HideSolutionNode = FALSE
27+
EndGlobalSection
28+
GlobalSection(ExtensibilityGlobals) = postSolution
29+
SolutionGuid = {A57075D5-CCA0-4D40-AF1E-6593C3D1BE60}
30+
EndGlobalSection
31+
EndGlobal
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Text.RegularExpressions;
7+
using System.Xml;
8+
using HtmlAgilityPack;
9+
10+
namespace Selenium.WebDriver.Linq
11+
{
12+
internal static class HtmlElementHelper
13+
{
14+
internal static Dictionary<string, string> GetAttributes(string htmlElement)
15+
{
16+
Dictionary<string, string> attributes = new Dictionary<string, string>();
17+
18+
if (string.IsNullOrEmpty(htmlElement))
19+
return attributes;
20+
21+
HtmlDocument doc = new HtmlDocument();
22+
doc.LoadHtml(htmlElement);
23+
24+
foreach (var attribute in doc.DocumentNode.FirstChild.Attributes)
25+
{
26+
attributes.Add(attribute.Name, attribute.Value);
27+
}
28+
29+
return attributes;
30+
}
31+
}
32+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
using OpenQA.Selenium;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Collections.ObjectModel;
5+
using System.Drawing;
6+
using Selenium.WebDriver.SimpleExtensions;
7+
using System.Linq;
8+
using System.Xml.Linq;
9+
10+
namespace Selenium.WebDriver.Linq
11+
{
12+
public class LinqWebElement : WebElement
13+
{
14+
private readonly IWebElement _webElement;
15+
16+
public LinqWebElement(IWebElement webElement,OpenQA.Selenium.WebDriver parentDriver, string id) : base(parentDriver, id)
17+
{
18+
_webElement = webElement;
19+
}
20+
21+
public string Id
22+
{
23+
get { return _webElement.GetAttribute("id"); }
24+
set
25+
{
26+
IJavaScriptExecutor js = (IJavaScriptExecutor)base.WrappedDriver;
27+
js.ExecuteScript($"arguments[0].setAttribute('id', '{value}')", _webElement);
28+
}
29+
}
30+
31+
public string Class
32+
{
33+
get { return _webElement.GetAttribute("class"); }
34+
set
35+
{
36+
IJavaScriptExecutor js = (IJavaScriptExecutor)base.WrappedDriver;
37+
js.ExecuteScript($"arguments[0].setAttribute('class', '{value}')", _webElement);
38+
}
39+
}
40+
41+
public string Style
42+
{
43+
get { return _webElement.GetAttribute("style"); }
44+
set
45+
{
46+
IJavaScriptExecutor js = (IJavaScriptExecutor)base.WrappedDriver;
47+
js.ExecuteScript($"arguments[0].setAttribute('style', '{value}')", _webElement);
48+
}
49+
}
50+
51+
public LinqWebElement Parent => _webElement.GetParent().AsLinqWebElement();
52+
53+
public IEnumerable<LinqWebElement> Children => _webElement.GetChildren().Select(s => s.AsLinqWebElement());
54+
55+
public IEnumerable<LinqWebElement> Siblings => _webElement.GetSiblings().Select(s => s.AsLinqWebElement());
56+
57+
public void Delete()
58+
{
59+
_webElement.Delete();
60+
}
61+
62+
public void SetAttribute(string attribute, string value)
63+
{
64+
_webElement.SetAttribute(attribute, value);
65+
}
66+
67+
public bool HasAttribute(string attribute)
68+
{
69+
if (string.IsNullOrEmpty(attribute))
70+
return false;
71+
72+
return Attributes.Any(a=>a.Key == attribute.ToLower());
73+
}
74+
75+
public void SetText(string text)
76+
{
77+
_webElement.SetText(text);
78+
}
79+
80+
public void AppendHtml(string html)
81+
{
82+
_webElement.AppendHtml(html);
83+
}
84+
85+
public Dictionary<string,string> Attributes
86+
{
87+
get
88+
{
89+
var html = _webElement.GetAttribute("outerHTML");
90+
return HtmlElementHelper.GetAttributes(html);
91+
}
92+
}
93+
}
94+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netstandard2.0</TargetFramework>
5+
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<PackageReference Include="HtmlAgilityPack" Version="1.11.46" />
10+
<PackageReference Include="Selenium.WebDriver" Version="4.9.1" />
11+
<PackageReference Include="Selenium.WebDriver.SimpleExtensions" Version="1.0.3" />
12+
</ItemGroup>
13+
14+
</Project>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using OpenQA.Selenium;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Text;
5+
6+
namespace Selenium.WebDriver.Linq
7+
{
8+
public static class WebElementExtensions
9+
{
10+
public static LinqWebElement AsLinqWebElement(this IWebElement element)
11+
{
12+
if (element == null)
13+
return null;
14+
15+
var elem = (WebElement)element;
16+
string id = elem.GetAttribute("id");
17+
return new LinqWebElement(element, elem.WrappedDriver as OpenQA.Selenium.WebDriver, id);
18+
}
19+
}
20+
}

TestConsole/Program.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using OpenQA.Selenium;
2+
using OpenQA.Selenium.Chrome;
3+
using Selenium.WebDriver.Linq;
4+
5+
namespace TestConsole
6+
{
7+
class Program
8+
{
9+
static void Main(string[] args)
10+
{
11+
ChromeDriver driver = new ChromeDriver();
12+
13+
driver.Navigate().GoToUrl("https://abv.bg");
14+
15+
var elem = driver.FindElement(By.TagName("body")).AsLinqWebElement();
16+
17+
var test = elem.Children.First(w=> w.Id == "siteContent");
18+
19+
var cc = test.Children.ToList();
20+
21+
22+
}
23+
}
24+
}

TestConsole/TestConsole.csproj

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net6.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="HtmlAgilityPack" Version="1.11.46" />
12+
<PackageReference Include="Selenium.WebDriver" Version="4.9.1" />
13+
<PackageReference Include="Selenium.WebDriver.ChromeDriver" Version="113.0.5672.6300" />
14+
</ItemGroup>
15+
16+
<ItemGroup>
17+
<ProjectReference Include="..\Selenium.WebDriver.Linq\Selenium.WebDriver.Linq.csproj" />
18+
</ItemGroup>
19+
20+
</Project>

0 commit comments

Comments
 (0)