Skip to content

Commit

Permalink
add try-finally
Browse files Browse the repository at this point in the history
  • Loading branch information
Lifailon authored Oct 19, 2023
1 parent 0bd0e8f commit 0006165
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 12 deletions.
21 changes: 16 additions & 5 deletions Modules/Get-FreeGPT.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,21 @@ param (
$path = "$home\Documents\Selenium\"
$ChromeDriver = "$path\ChromeDriver.exe"
$WebDriver = "$path\WebDriver.dll"
$SupportDriver = "$path\WebDriver.Support.dll"
$Chromium = (Get-ChildItem $path -Recurse | Where-Object Name -like chrome.exe).FullName
Add-Type -Path $WebDriver
Add-Type -Path $SupportDriver
try {
$ChromeOptions = New-Object OpenQA.Selenium.Chrome.ChromeOptions
$ChromeOptions.BinaryLocation = $Chromium
$ChromeOptions.AddArgument("start-maximized")
$ChromeOptions.AcceptInsecureCertificates = $True
$ChromeOptions.AddArgument("headless")
$Selenium = New-Object OpenQA.Selenium.Chrome.ChromeDriver($ChromeDriver, $ChromeOptions)
$Selenium.Navigate().GoToUrl("$Url") > $null
$Selenium.Navigate().GoToUrl("$Url")
Start-Sleep 1
$Limit = $Selenium.FindElements([OpenQA.Selenium.By]::ClassName("quota-wrapper")).Text
Write-Host "Limit: $Limit" -ForegroundColor Green
$textarea = $Selenium.FindElements([OpenQA.Selenium.By]::TagName("textarea"))
$textarea.SendKeys($text)
$button = $Selenium.FindElements([OpenQA.Selenium.By]::TagName("button"))
Expand All @@ -31,11 +36,17 @@ while ($True) {
break
}
}
}
finally {
$Selenium.Close()
$Selenium.Quit()
}
}

$Result = Get-FreeGPT -Text "22+33"
$Eng = "Hello, my friend"
$Result = Get-FreeGPT -Text "Translate the text into Russian: $Eng"
Write-Host $Result -ForegroundColor Green
# Example 1:
# $Result = Get-FreeGPT -Text "22+33"
# Write-Host $Result -ForegroundColor Green
# Example 2:
# $Eng = "Hello, my friend"
# $Result = Get-FreeGPT -Text "Translate the text into Russian: $Eng"
# Write-Host $Result -ForegroundColor Green
12 changes: 9 additions & 3 deletions Modules/Get-SpeedTest.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ $SupportDriver = "$path\WebDriver.Support.dll"
$Chromium = (Get-ChildItem $path -Recurse | Where-Object Name -like chrome.exe).FullName
Add-Type -Path $WebDriver
Add-Type -Path $SupportDriver
try {
$ChromeOptions = New-Object OpenQA.Selenium.Chrome.ChromeOptions
$ChromeOptions.BinaryLocation = $Chromium
$ChromeOptions.AddArgument("start-maximized")
$ChromeOptions.AcceptInsecureCertificates = $True
#$ChromeOptions.AddArgument("headless")
$ChromeOptions.AddArgument("headless")
$Selenium = New-Object OpenQA.Selenium.Chrome.ChromeDriver($ChromeDriver, $ChromeOptions)
if ($Provider -eq "Ookla") {
$Url = "https://www.speedtest.net/"
Expand Down Expand Up @@ -116,11 +117,16 @@ if ($Provider -eq "Libre") {
})
return $Collections
}
}
finally {
$Selenium.Close()
$Selenium.Quit()
}
}

# Example:
# Get-SpeedTest -Provider Ookla
# Get-SpeedTest -Provider Libre
# Get-SpeedTest -Provider Open
# Get-SpeedTest -Provider Libre
# $Test = Get-SpeedTest -Provider Ookla
# $Test | Out-Default
# $Test.additional_servers
11 changes: 7 additions & 4 deletions Modules/Get-Translate.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ $SupportDriver = "$path\WebDriver.Support.dll"
$Chromium = (Get-ChildItem $path -Recurse | Where-Object Name -like chrome.exe).FullName
Add-Type -Path $WebDriver
Add-Type -Path $SupportDriver
try {
$ChromeOptions = New-Object OpenQA.Selenium.Chrome.ChromeOptions
$ChromeOptions.BinaryLocation = $Chromium
$ChromeOptions.AddArgument("start-maximized")
Expand All @@ -22,9 +23,7 @@ $Selenium.Navigate().GoToUrl($Url)
Start-Sleep 1
$div = $Selenium.FindElements([OpenQA.Selenium.By]::TagName("div"))
$InTextBox = $div | Where-Object ComputedAccessibleRole -Match "TextBox" | Where-Object ComputedAccessibleLabel -Match "Исходный текст"
#$InTextBox = $Selenium.FindElements([OpenQA.Selenium.By]::XPath('//*[@id="headlessui-tabs-panel-7"]/div/div[1]/section/div/div[2]/div[1]/section/div/div[1]/d-textarea/div[1]'))
$OutTextBox = $div | Where-Object ComputedAccessibleRole -Match "TextBox" | Where-Object ComputedAccessibleLabel -Match "Переведенный текст"
#$OutTextBox = $Selenium.FindElements([OpenQA.Selenium.By]::XPath('//*[@id="headlessui-tabs-panel-7"]/div/div[1]/section/div/div[2]/div[3]/section/div[1]/d-textarea/div'))
$OutTemp = $OutTextBox.Text
$InTextBox.SendKeys($text)
while ($True) {
Expand All @@ -33,9 +32,13 @@ while ($True) {
break
}
}
}
finally {
$Selenium.Close()
$Selenium.Quit()
}
}

$Result = Get-Translate -Text "Hello, my friend"
Write-Host $Result -ForegroundColor Green
# Example:
# $Result = Get-Translate -Text "Hello, my friend"
# Write-Host $Result -ForegroundColor Green

0 comments on commit 0006165

Please # to comment.