Skip to content

Commit 7120bef

Browse files
authored
Merge pull request #18 from LarrysGIT/master
Add a new cmdlet 'Get-SeKeys' to list supported keys, update 'Send-Se…
2 parents 5028eec + 84158d0 commit 7120bef

File tree

3 files changed

+56
-2
lines changed

3 files changed

+56
-2
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@
1010
Install-Module Selenium
1111
```
1212

13+
OR
14+
15+
```
16+
Import-Module "{FullPath}\selenium-powershell\Selenium.psm1"
17+
```
18+
1319
# Usage
1420

1521
## Navigate to a URL
@@ -49,4 +55,12 @@ Send-SeKeys -Element $Element -Keys "[email protected]"
4955

5056
```powershell
5157
$Driver = Start-SeChrome -Arguments "headless","incognito"
58+
```
59+
60+
## Wait for an element
61+
```powershell
62+
$Driver = Start-SeChrome
63+
Enter-SeUrl https://www.google.com -Driver $Driver
64+
Wait-SeElementExists -Driver $Driver -Timeout 3 -Id "q"
65+
Wait-SeElementExists -Driver $Driver -Timeout 3 -Name "q"
5266
```

Selenium.psd1

96 Bytes
Binary file not shown.

Selenium.psm1

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,17 @@
33
function Start-SeChrome {
44
Param(
55
[Parameter(Mandatory = $false)]
6-
[array]$Arguments
6+
[array]$Arguments,
7+
[switch]$HideVersionHint
78
)
89
if($Arguments) {
910
$Chrome_Options = New-Object -TypeName "OpenQA.Selenium.Chrome.ChromeOptions"
1011
$Chrome_Options.AddArguments($Arguments)
1112
}
13+
if(!$HideVersionHint)
14+
{
15+
Write-Host "Download the right chromedriver from 'http://chromedriver.chromium.org/downloads'" -ForegroundColor Yellow
16+
}
1217
New-Object -TypeName "OpenQA.Selenium.Chrome.ChromeDriver" -ArgumentList $Chrome_Options
1318
}
1419

@@ -121,9 +126,18 @@ function Invoke-SeClick {
121126

122127
}
123128

129+
function Get-SeKeys {
130+
131+
[OpenQA.Selenium.Keys] | Get-Member -MemberType Property -Static | Select-Object -Property Name,@{N = "ObjectString"; E = {"[OpenQA.Selenium.Keys]::$($_.Name)"}}
132+
}
133+
124134
function Send-SeKeys {
125135
param([OpenQA.Selenium.IWebElement]$Element, [string]$Keys)
126-
136+
137+
foreach($Key in @(Get-SeKeys).Name)
138+
{
139+
$Keys = $Keys -replace "{{$Key}}", [OpenQA.Selenium.Keys]::$Key
140+
}
127141
$Element.SendKeys($Keys)
128142
}
129143

@@ -184,3 +198,29 @@ function Save-SeScreenshot {
184198
$Screenshot.SaveAsFile($Path, $ImageFormat)
185199
}
186200
}
201+
202+
function Wait-SeElementExists {
203+
param(
204+
$Driver,
205+
$Timeout = 30,
206+
$Id,
207+
$Name
208+
)
209+
if($Id)
210+
{
211+
$TargetElement = [OpenQA.Selenium.By]::Id($Id)
212+
}
213+
elseif($Name)
214+
{
215+
$TargetElement = [OpenQA.Selenium.By]::Name($Name)
216+
}
217+
else
218+
{
219+
throw "Please specify -Id or -Name"
220+
}
221+
$WebDriverWait = New-Object OpenQA.Selenium.Support.UI.WebDriverWait $Driver, ($Timeout * 10000000) # ticks
222+
$Condition = [OpenQA.Selenium.Support.UI.ExpectedConditions]::ElementExists($TargetElement)
223+
$WebDriverWait.Until($Condition)
224+
}
225+
226+

0 commit comments

Comments
 (0)