Powershell Selenium IE-单击innerHTML元素

Powershell Selenium IE-单击innerHTML元素,powershell,selenium,internet-explorer,click,innerhtml,Powershell,Selenium,Internet Explorer,Click,Innerhtml,我正在尝试在powershell中创建一个脚本,该脚本将自动单击我的作业本地页面。但是,单击此元素时出现问题: 我尝试了很多东西,但不知道下一步该尝试什么。。。 如果您需要更多信息,请帮助我并告诉我。如果只有一个元素的文本为“应用程序管理”,您可以使用以下内容找到该元素: $Driver = ... # Setup Selenium driver $Element = Find-SeElement -Driver $Driver -TagName A | Where-Object { $_.

我正在尝试在powershell中创建一个脚本,该脚本将自动单击我的作业本地页面。但是,单击此元素时出现问题:


我尝试了很多东西,但不知道下一步该尝试什么。。。
如果您需要更多信息,请帮助我并告诉我。

如果只有一个元素的文本为“应用程序管理”,您可以使用以下内容找到该元素:

$Driver = ... # Setup Selenium driver
$Element = Find-SeElement -Driver $Driver -TagName A | Where-Object { $_.Text -eq 'Application Management' }
Invoke-SeClick -Element $Element # Click the element

请参考以下代码,使用
findelementbypath
方法根据文本值查找元素

$link = $seleniumDriver.FindElementByXPath("//a[contains(text(),'Application Management')]")

$link.Click()
详细示例代码如下所示:

#add references to the Selenium DLLs 
$WebDriverPath = Resolve-Path "E:\Projects\PowerShellSamples\WebDriver.dll"
#I unblock it because when you download a DLL from a remote source it is often blocked by default
Unblock-File $WebDriverPath
Add-Type -Path $WebDriverPath

$WebDriverSupportPath = Resolve-Path "E:\Projects\PowerShellSamples\WebDriver.Support.dll"
Unblock-File $WebDriverSupportPath
Add-Type -Path $WebDriverSupportPath

#before we start, we must ensure all zones are running either in protected mode, or not.  They need to all be the same.
#(we might be able to negate the requirement for some of these using InternetExplorerOptions.IntroduceInstabilityByIgnoringProtectedModeSettings)

#set protected
#local
#New-ItemProperty "hkcu:\Software\Microsoft\Windows\CurrentVersion\Internet Settings\Zones\0" -Name "2500" -Value 0 -PropertyType DWORD -Force | Out-Null
#internet
#New-ItemProperty "hkcu:\Software\Microsoft\Windows\CurrentVersion\Internet Settings\Zones\1" -Name "2500" -Value 0 -PropertyType DWORD -Force | Out-Null
#intranet
#New-ItemProperty "hkcu:\Software\Microsoft\Windows\CurrentVersion\Internet Settings\Zones\2" -Name "2500" -Value 0 -PropertyType DWORD -Force | Out-Null
#trusted
#New-ItemProperty "hkcu:\Software\Microsoft\Windows\CurrentVersion\Internet Settings\Zones\3" -Name "2500" -Value 0 -PropertyType DWORD -Force | Out-Null
#restricted
#New-ItemProperty "hkcu:\Software\Microsoft\Windows\CurrentVersion\Internet Settings\Zones\4" -Name "2500" -Value 0 -PropertyType DWORD -Force | Out-Null

#can pass this stuff in when we instantiate driver if needs be (if we want a chromeless browser for example)
$seleniumOptions = New-Object OpenQA.Selenium.IE.InternetExplorerOptions
#open this URL when Internet Explorer launches
$seleniumOptions.InitialBrowserUrl = "<the website url>";


#provide the selenium options to the Internett Explorer driver (calling this opens the IE session)
$seleniumDriver = New-Object OpenQA.Selenium.IE.InternetExplorerDriver -ArgumentList @($seleniumOptions)

#click button 

$link = $seleniumDriver.FindElementByXPath("//a[contains(text(),'Application Management')]")

$link.Click()

#we don't close it in this instance because we want to keep the browser open as a dashboard view
#$seleniumDriver.Close()
#$seleniumDriver.Dispose()
#$seleniumDriver.Quit()
<a onclick="javascript:alert('hello')" class="Text" href="#" name="null">Application Management</a>
#添加对Selenium DLL的引用
$WebDriverPath=解析路径“E:\Projects\PowerShellSamples\WebDriver.dll”
#我取消阻止它是因为当您从远程源下载DLL时,默认情况下它通常被阻止
取消阻止文件$WebDriverPath
添加类型-路径$WebDriverPath
$WebDriversSupportPath=解析路径“E:\Projects\PowerShellSamples\WebDriver.Support.dll”
取消阻止文件$WebDriversSupportPath
添加类型-路径$WebDriversSupportPath
#在开始之前,我们必须确保所有区域都在保护模式下运行,或者不在保护模式下运行。它们必须是一样的。
#(我们可以使用InternetExplorerProptions.IntroductionInstabilityByIgnoringProtectedModesettings来否定其中一些的要求)
#设置保护
#本地的
#新项目属性“hkcu:\Software\Microsoft\Windows\CurrentVersion\Internet设置\Zones\0”-名称“2500”-值0-属性类型DWORD-强制输出空值
#互联网
#新项目属性“hkcu:\Software\Microsoft\Windows\CurrentVersion\Internet设置\Zones\1”-名称“2500”-值0-属性类型DWORD-强制输出空值
#内部网
#新项目属性“hkcu:\Software\Microsoft\Windows\CurrentVersion\Internet设置\Zones\2”-名称“2500”-值0-属性类型DWORD-强制输出空值
#信任
#新项目属性“hkcu:\Software\Microsoft\Windows\CurrentVersion\Internet设置\Zones\3”-名称“2500”-值0-属性类型DWORD-强制输出空值
#受限的
#新项目属性“hkcu:\Software\Microsoft\Windows\CurrentVersion\Internet设置\Zones\4”-名称“2500”-值0-属性类型DWORD-强制输出空值
#如果需要,我们可以在实例化驱动程序时传入这些内容(例如,如果我们想要无铬浏览器)
$seleniumOptions=新对象OpenQA.Selenium.IE.InternetExploreProptions
#Internet Explorer启动时打开此URL
$seleniumOptions.InitialBrowserUrl=“”;
#为Internett Explorer驱动程序提供selenium选项(调用此选项将打开IE会话)
$seleniumDriver=新对象OpenQA.Selenium.IE.InternetExplorerDriver-ArgumentList@($seleniumOptions)
#点击按钮
$link=$seleniumDriver.findelementbypath(“//a[contains(text(),'Application Management')]”)
$link.Click()
#在本例中,我们不关闭它,因为我们希望保持浏览器作为仪表板视图打开
#$seleniumDriver.Close()
#$seleniumDriver.Dispose()
#$seleniumDriver.Quit()
编辑:在我的示例中,网站资源如下所示:

#add references to the Selenium DLLs 
$WebDriverPath = Resolve-Path "E:\Projects\PowerShellSamples\WebDriver.dll"
#I unblock it because when you download a DLL from a remote source it is often blocked by default
Unblock-File $WebDriverPath
Add-Type -Path $WebDriverPath

$WebDriverSupportPath = Resolve-Path "E:\Projects\PowerShellSamples\WebDriver.Support.dll"
Unblock-File $WebDriverSupportPath
Add-Type -Path $WebDriverSupportPath

#before we start, we must ensure all zones are running either in protected mode, or not.  They need to all be the same.
#(we might be able to negate the requirement for some of these using InternetExplorerOptions.IntroduceInstabilityByIgnoringProtectedModeSettings)

#set protected
#local
#New-ItemProperty "hkcu:\Software\Microsoft\Windows\CurrentVersion\Internet Settings\Zones\0" -Name "2500" -Value 0 -PropertyType DWORD -Force | Out-Null
#internet
#New-ItemProperty "hkcu:\Software\Microsoft\Windows\CurrentVersion\Internet Settings\Zones\1" -Name "2500" -Value 0 -PropertyType DWORD -Force | Out-Null
#intranet
#New-ItemProperty "hkcu:\Software\Microsoft\Windows\CurrentVersion\Internet Settings\Zones\2" -Name "2500" -Value 0 -PropertyType DWORD -Force | Out-Null
#trusted
#New-ItemProperty "hkcu:\Software\Microsoft\Windows\CurrentVersion\Internet Settings\Zones\3" -Name "2500" -Value 0 -PropertyType DWORD -Force | Out-Null
#restricted
#New-ItemProperty "hkcu:\Software\Microsoft\Windows\CurrentVersion\Internet Settings\Zones\4" -Name "2500" -Value 0 -PropertyType DWORD -Force | Out-Null

#can pass this stuff in when we instantiate driver if needs be (if we want a chromeless browser for example)
$seleniumOptions = New-Object OpenQA.Selenium.IE.InternetExplorerOptions
#open this URL when Internet Explorer launches
$seleniumOptions.InitialBrowserUrl = "<the website url>";


#provide the selenium options to the Internett Explorer driver (calling this opens the IE session)
$seleniumDriver = New-Object OpenQA.Selenium.IE.InternetExplorerDriver -ArgumentList @($seleniumOptions)

#click button 

$link = $seleniumDriver.FindElementByXPath("//a[contains(text(),'Application Management')]")

$link.Click()

#we don't close it in this instance because we want to keep the browser open as a dashboard view
#$seleniumDriver.Close()
#$seleniumDriver.Dispose()
#$seleniumDriver.Quit()
<a onclick="javascript:alert('hello')" class="Text" href="#" name="null">Application Management</a>


请发布您已经尝试过的代码。我希望这能起作用,但它只是中断了:Find-SeElement:术语“Find-SeElement”不能识别为cmdlet、函数、脚本文件或可操作程序的名称。请检查名称的拼写,或者如果包含路径,请验证路径是否正确,然后重试。我想我应该补充一下,我正在使用selenium powershell模块。但这确实有效;我用过这个。对我不起作用:使用“1”参数调用“FindElementByXPath”时出现异常:“无法找到xpath=//a[contains(text(),'Application Management')]”的元素。请尝试使用F12开发工具检查html元素,无论它是否包含此链接,在我的示例中html资源如下: