Ich glaube selenium kann dir helfen, ich habe mal meine Selenium Klasse eingefügt, dass du dir da was besser vorstellen kannst.
//
using OpenQA.Selenium;
using OpenQA.Selenium.Support.UI;
using OpenQA.Selenium.Chrome;
using System;
using System.Collections.ObjectModel;
using System.Drawing;
using System.Drawing.Imaging;
using System.Threading;
using System.IO;
namespace Rupert {
public class Selenium {
public ChromeDriver driver;
public WebDriverWait wait;
public Selenium() {
}
public string currentUrl {
get => driver.Url;
}
public void Close_Browser() {
Rupert.Logger.I("Close_Browser");
if(driver != null)
driver.Quit();
}
public ReadOnlyCollection<IWebElement> DoFindElementsByClassNames(string ClassName) => driver.FindElements(By.ClassName(ClassName));
public Screenshot DoGetScreenShot() => ((ITakesScreenshot) driver).GetScreenshot();
public IWebElement DoFindElementByXPath(string XPath) => driver.FindElement(By.XPath(XPath));
public ReadOnlyCollection<IWebElement> DoFindElementsByXPath(string XPath) => driver.FindElements(By.XPath(XPath));
public IWebElement DoFindElementByName(string Name) => driver.FindElement(By.Name(Name));
private static IJavaScriptExecutor JavaScriptExecutor;
public static object EXJSScript(string JSScript, params object[] args) => JavaScriptExecutor?.ExecuteScript(JSScript, args);
private readonly object syncLock = new object();
/// <summary>
///
/// </summary>
/// <param name="pWebElement"></param>
/// <param name="pFileDir"></param>
/// <param name="pFilename"></param>
/// <returns></returns>
public string? DoSaveNodeScreenshot(IWebElement pWebElement, string pFileDir, string pFilename) {
try {
//
Byte[] byteArray;
int Left;
int Top;
int height;
int width;
string savedir = $"{pFileDir.Trim()}{pFilename.Trim()}.{ImageFormat.Jpeg}"; //
//
lock(syncLock) {
EXJSScript("arguments[0].scrollIntoView(true);", pWebElement);
Left = Convert.ToInt32(EXJSScript("return arguments[0].getBoundingClientRect().x", pWebElement));
Top = Convert.ToInt32(EXJSScript("return arguments[0].getBoundingClientRect().y", pWebElement));
height = Convert.ToInt32(EXJSScript("return arguments[0].getBoundingClientRect().height", pWebElement));
width = Convert.ToInt32(EXJSScript("return arguments[0].getBoundingClientRect().width", pWebElement));
// No Image Load = No Screenshot
if(Left < 0 && Top < 0 && height < 0 && width < 0)
return default;
// Thread.Sleep(50);
byteArray = DoGetScreenShot().AsByteArray;
}
using MemoryStream MemStream = new MemoryStream(byteArray);
Bitmap screenshot = new Bitmap(MemStream);
Rectangle croppedImage = new Rectangle(Left, Top, height, width);
Bitmap image = screenshot.Clone(croppedImage, screenshot.PixelFormat);
screenshot.Dispose();
image.Save(savedir);
image.Dispose();
//
Logger.I($"Image Saved at: {savedir}");
return savedir;
} catch(Exception Ex) {
Logger.E("DoSaveNodeScreenshot", Ex);
return default;
}
}
public IWebElement doFindElementByClassNames(String ClassName) => driver.FindElement(By.ClassName(ClassName));
public IWebElement doFindElementByCSSSelector(String CSSSelector) => driver.FindElement(By.CssSelector(CSSSelector));
public ReadOnlyCollection<IWebElement> doFindElementsByCSSSelector(String CSSSelector) => driver.FindElements(By.CssSelector(CSSSelector));
public void ResetNetworkConditions() => SetNetworkConditions(DoGetChromeNetworkConditions());
private void SetNetworkConditions(ChromeNetworkConditions val) {
driver.NetworkConditions = val;
Logger.I($"SetNetworkConditions: {val}");
}
public void TurnOffConnection() {
SetNetworkConditions(DoGetChromeNetworkConditions(true));
}
public String Navigate(Uri Uri) {
if(driver == null) {
start_Browser();
}
driver.Url = Uri.AbsoluteUri;
return driver.Url;
}
private ChromeNetworkConditions DoGetChromeNetworkConditions(
bool offline = false,
long download = long.MaxValue,
long upload = long.MaxValue,
TimeSpan? latency = null
) {
return new ChromeNetworkConditions() {
IsOffline = offline,
DownloadThroughput = download,
Latency = (latency != null) ? (TimeSpan) latency : new TimeSpan(1), // muss man anscheinden so machen 🤮
UploadThroughput = upload,
};
}
public void start_Browser() {
if(driver != null) {
return;
}
//
var service = ChromeDriverService.CreateDefaultService(@"C:\Program Files\Google\Chrome\Application\");
service.LogPath = BaseClass.DesktopPath + @"\chrome.log";
service.EnableVerboseLogging = true;
// Local Selenium WebDriver
// from selenium import webdriver
// driver = webdriver.Chrome(executable_path = r'C:\path\to\chromedriver.exe');
// driver.get('http://google.com/');
ChromeOptions options = new ChromeOptions();
options.AddArguments("start-maximized"); // open Browser in maximized mode
options.AddArguments("disable-infobars"); // disabling infobars
options.AddArguments("--disable-extensions"); // disabling extensions
options.AddArguments("--disable-gpu"); // applicable to windows os only
options.AddArguments("--disable-dev-shm-usage"); // overcome limited resource problems
// options.AddArguments("--no-sandbox"); // Bypass OS security model
driver = new ChromeDriver(service, options);
wait = new WebDriverWait(driver, TimeSpan.FromSeconds(60));
JavaScriptExecutor = (IJavaScriptExecutor) driver;
}
}
}