PowerShell SendKey 函数上的 Selenium Webdriver 在当前活动 window 上发送键,而不是 Selenium 调用的浏览器
Selenium Webdriver on PowerShell SendKey function send keys on current active window, not the Selenium invoked browser
我正在编写运行 Selenium Webdriver 的 PowerShell 脚本。我打算做的是打开一个页面,再打开 x 个标签,return 到第一个标签,做点什么,转到下一个标签,做点什么,等等。
但是当我在代码上使用 SendKeys 发送 Ctrl+T 以打开一个新选项卡时,除非 Selenium 调用的浏览器处于活动状态,否则它将 Ctrl+T 发送到我的活动 window,(例如, Powershell ISE(它的作用是打开一个新的 PowerShell 选项卡))。
我希望你们能帮助我,伙计们。
代码如下:
Add-Type -AssemblyName "System.Windows.Forms"
Set-Location "$PSScriptRoot\selenium-dotnet-2.53.0\net35"
[System.Reflection.Assembly]::LoadFrom("$PSScriptRoot\selenium-dotnet-2.53.0\net35\Selenium.WebDriverBackedSelenium.dll")
[System.Reflection.Assembly]::LoadFrom("$PSScriptRoot\selenium-dotnet-2.53.0\net35\ThoughtWorks.Selenium.Core.dll")
[System.Reflection.Assembly]::LoadFrom("$PSScriptRoot\selenium-dotnet-2.53.0\net35\WebDriver.dll")
[System.Reflection.Assembly]::LoadFrom("$PSScriptRoot\selenium-dotnet-2.53.0\net35\WebDriver.Support.dll")
$servers = `
"http://facebook.com", `
"http://google.com"
$driver = New-Object "OpenQA.Selenium.FireFox.FirefoxDriver"
$driver.Manage().Window.Maximize();
$actions = New-Object OpenQA.Selenium.Interactions.Actions($driver)
for ($i = 0; $i -lt $($servers.Length); $i++){
$driver.Navigate().GoToUrl($servers[$i])
if ($i + 1 -ne $($servers.Length)) {
$driver.FindElementByCssSelector("body").SendKeys($body,[System.Windows.Forms.SendKeys]::SendWait("^t")) | Out-Null
}
}
foreach($server in $servers){
$driver.FindElementByCssSelector("body").SendKeys($body, [System.Windows.Forms.SendKeys]::SendWait("^{TAB}")) | Out-Null
$driver.SwitchTo().DefaultContent()
}
您可以检查 Appactive() 方法或检查当前活动 window 并继续。
编辑:您可以在 selenium 打开一个浏览器会话之前终止其他浏览器会话,并使用 Appactive 使我处于活动状态 windows。
此致,
kvprasoon
代替这两个 SendKeys 命令,尝试使用:
$driver.executeScript("window.open('http://YourDomainGoesHere.com/', '_blank')")
适用于 IE,但我自己还没有在 Firefox 上尝试过。
我正在编写运行 Selenium Webdriver 的 PowerShell 脚本。我打算做的是打开一个页面,再打开 x 个标签,return 到第一个标签,做点什么,转到下一个标签,做点什么,等等。
但是当我在代码上使用 SendKeys 发送 Ctrl+T 以打开一个新选项卡时,除非 Selenium 调用的浏览器处于活动状态,否则它将 Ctrl+T 发送到我的活动 window,(例如, Powershell ISE(它的作用是打开一个新的 PowerShell 选项卡))。
我希望你们能帮助我,伙计们。
代码如下:
Add-Type -AssemblyName "System.Windows.Forms"
Set-Location "$PSScriptRoot\selenium-dotnet-2.53.0\net35"
[System.Reflection.Assembly]::LoadFrom("$PSScriptRoot\selenium-dotnet-2.53.0\net35\Selenium.WebDriverBackedSelenium.dll")
[System.Reflection.Assembly]::LoadFrom("$PSScriptRoot\selenium-dotnet-2.53.0\net35\ThoughtWorks.Selenium.Core.dll")
[System.Reflection.Assembly]::LoadFrom("$PSScriptRoot\selenium-dotnet-2.53.0\net35\WebDriver.dll")
[System.Reflection.Assembly]::LoadFrom("$PSScriptRoot\selenium-dotnet-2.53.0\net35\WebDriver.Support.dll")
$servers = `
"http://facebook.com", `
"http://google.com"
$driver = New-Object "OpenQA.Selenium.FireFox.FirefoxDriver"
$driver.Manage().Window.Maximize();
$actions = New-Object OpenQA.Selenium.Interactions.Actions($driver)
for ($i = 0; $i -lt $($servers.Length); $i++){
$driver.Navigate().GoToUrl($servers[$i])
if ($i + 1 -ne $($servers.Length)) {
$driver.FindElementByCssSelector("body").SendKeys($body,[System.Windows.Forms.SendKeys]::SendWait("^t")) | Out-Null
}
}
foreach($server in $servers){
$driver.FindElementByCssSelector("body").SendKeys($body, [System.Windows.Forms.SendKeys]::SendWait("^{TAB}")) | Out-Null
$driver.SwitchTo().DefaultContent()
}
您可以检查 Appactive() 方法或检查当前活动 window 并继续。
编辑:您可以在 selenium 打开一个浏览器会话之前终止其他浏览器会话,并使用 Appactive 使我处于活动状态 windows。
此致,
kvprasoon
代替这两个 SendKeys 命令,尝试使用:
$driver.executeScript("window.open('http://YourDomainGoesHere.com/', '_blank')")
适用于 IE,但我自己还没有在 Firefox 上尝试过。