Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace Playwright with Selenium #381

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions GhostfolioSidekick/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using RestSharp;
using System.Diagnostics.CodeAnalysis;

Expand Down
39 changes: 20 additions & 19 deletions Tools/ScraperUtilities/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
using GhostfolioSidekick.Model.Activities;
using GhostfolioSidekick.Model.Activities.Types;
using GhostfolioSidekick.Parsers.Generic;
using Microsoft.Playwright;
using OpenQA.Selenium;

Check failure on line 6 in Tools/ScraperUtilities/Program.cs

View workflow job for this annotation

GitHub Actions / build

The type or namespace name 'OpenQA' could not be found (are you missing a using directive or an assembly reference?)
using OpenQA.Selenium.Chrome;

Check failure on line 7 in Tools/ScraperUtilities/Program.cs

View workflow job for this annotation

GitHub Actions / build

The type or namespace name 'OpenQA' could not be found (are you missing a using directive or an assembly reference?)
using ScraperUtilities.ScalableCapital;
using System.Globalization;

Expand All @@ -14,26 +15,26 @@
static async Task Main(string[] args)
{
var arguments = CommandLineArguments.Parse(args);
var playWright = await Playwright.CreateAsync();
var browser = await playWright.Chromium.LaunchAsync(
new BrowserTypeLaunchOptions
{
Headless = false
});
var page = await browser.NewPageAsync();
var options = new ChromeOptions();
options.AddArgument("--headless");
options.AddArgument("--disable-gpu");
options.AddArgument("--no-sandbox");

IEnumerable<ActivityWithSymbol> transactions = [];
switch (arguments.Broker)
using (var driver = new ChromeDriver(options))
{
case "ScalableCapital":
var scraper = new Scraper(page, arguments);
transactions = await scraper.ScrapeTransactions();
break;
default:
throw new ArgumentException("Invalid broker entered.");
}
IEnumerable<ActivityWithSymbol> transactions = [];
switch (arguments.Broker)
{
case "ScalableCapital":
var scraper = new Scraper(driver, arguments);
transactions = await scraper.ScrapeTransactions();
break;
default:
throw new ArgumentException("Invalid broker entered.");
}

SaveToCSV(arguments.OutputFile, transactions);
SaveToCSV(arguments.OutputFile, transactions);
}
}

private static void SaveToCSV(string outputFile, IEnumerable<ActivityWithSymbol> transactions)
Expand Down Expand Up @@ -136,4 +137,4 @@
return moneys.Sum(x => x.Amount);
}
}
}
}
34 changes: 23 additions & 11 deletions Tools/ScraperUtilities/ScalableCapital/Login.cs
Original file line number Diff line number Diff line change
@@ -1,33 +1,45 @@
using Microsoft.Playwright;
using OpenQA.Selenium;

Check failure on line 1 in Tools/ScraperUtilities/ScalableCapital/Login.cs

View workflow job for this annotation

GitHub Actions / build

The type or namespace name 'OpenQA' could not be found (are you missing a using directive or an assembly reference?)
using OpenQA.Selenium.Support.UI;

Check failure on line 2 in Tools/ScraperUtilities/ScalableCapital/Login.cs

View workflow job for this annotation

GitHub Actions / build

The type or namespace name 'OpenQA' could not be found (are you missing a using directive or an assembly reference?)
using SeleniumExtras.WaitHelpers;

Check failure on line 3 in Tools/ScraperUtilities/ScalableCapital/Login.cs

View workflow job for this annotation

GitHub Actions / build

The type or namespace name 'SeleniumExtras' could not be found (are you missing a using directive or an assembly reference?)

namespace ScraperUtilities.ScalableCapital
{
public class Login(IPage page, CommandLineArguments arguments)
public class Login
{
private readonly IWebDriver driver;
private readonly CommandLineArguments arguments;

public Login(IWebDriver driver, CommandLineArguments arguments)
{
this.driver = driver;
this.arguments = arguments;
}

public async Task<MainPage> LoginAsync()
{
await page.GotoAsync("https://de.scalable.capital/en/secure-login");
await page.Locator("#username").FillAsync(arguments.Username);
await page.Locator("#password").FillAsync(arguments.Password);
driver.Navigate().GoToUrl("https://de.scalable.capital/en/secure-login");

await page.ClickAsync("button[type='submit']");
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
wait.Until(ExpectedConditions.ElementIsVisible(By.Id("username"))).SendKeys(arguments.Username);
driver.FindElement(By.Id("password")).SendKeys(arguments.Password);
driver.FindElement(By.CssSelector("button[type='submit']")).Click();

// Wait for MFA
while (!await page.GetByTestId("greeting-text").IsVisibleAsync())
while (!driver.FindElement(By.CssSelector("[data-testid='greeting-text']")).Displayed)
{
Thread.Sleep(1000);
await Task.Delay(1000);
}

// Remove cookie banner
try
{
await page.GetByTestId("uc-accept-all-button").ClickAsync();
driver.FindElement(By.CssSelector("[data-testid='uc-accept-all-button']")).Click();
}
catch (Exception)
catch (NoSuchElementException)
{ // ignore
}

return new MainPage(page);
return new MainPage(driver);
}
}
}
19 changes: 11 additions & 8 deletions Tools/ScraperUtilities/ScalableCapital/MainPage.cs
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
using Microsoft.Playwright;
using OpenQA.Selenium;

Check failure on line 1 in Tools/ScraperUtilities/ScalableCapital/MainPage.cs

View workflow job for this annotation

GitHub Actions / build

The type or namespace name 'OpenQA' could not be found (are you missing a using directive or an assembly reference?)
using OpenQA.Selenium.Support.UI;

Check failure on line 2 in Tools/ScraperUtilities/ScalableCapital/MainPage.cs

View workflow job for this annotation

GitHub Actions / build

The type or namespace name 'OpenQA' could not be found (are you missing a using directive or an assembly reference?)
using SeleniumExtras.WaitHelpers;

Check failure on line 3 in Tools/ScraperUtilities/ScalableCapital/MainPage.cs

View workflow job for this annotation

GitHub Actions / build

The type or namespace name 'SeleniumExtras' could not be found (are you missing a using directive or an assembly reference?)

namespace ScraperUtilities.ScalableCapital
{
public class MainPage
{
private IPage page;
private IWebDriver driver;

public MainPage(IPage page)
public MainPage(IWebDriver driver)
{
this.page = page;
this.driver = driver;
}

internal async Task<TransactionPage> GoToTransactions()
{
await page.GotoAsync("https://de.scalable.capital/broker/transactions");
driver.Navigate().GoToUrl("https://de.scalable.capital/broker/transactions");

// Wait for transactions to load
await page.WaitForSelectorAsync("button:text('Export CSV')", new PageWaitForSelectorOptions { State = WaitForSelectorState.Visible });
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
wait.Until(ExpectedConditions.ElementIsVisible(By.CssSelector("button:text('Export CSV')")));

return new TransactionPage(page);
return new TransactionPage(driver);
}
}
}
}
Loading
Loading