使用powershell

时间:2016-11-16 05:54:45

标签: powershell scripting

enter image description here我在运行PowerShell脚本时收到以下错误消息

The property 'value' cannot be found on this object. Verify that the property exists and can be set.
At C:\Test.ps1:17 char:1
+ $usernamefield.value = $username
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : PropertyAssignmentException

The property 'value' cannot be found on this object. Verify that the property exists and can be set.
At C:\Test.ps1:20 char:1
+ $passwordfield.value = $password
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : PropertyAssignmentException

Method invocation failed because [System.DBNull] does not contain a method named 'click'.
At C:\Test.ps1:23 char:1
+ $Link.click()
+ ~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : MethodNotFound`

我的剧本:

$ie = New-Object -ComObject 'internetExplorer.Application'
$ie.Visible= $true # Make it visible

$usernmae="test"

$password="test1"

$ie.Navigate("https://website.com/login")

While ($ie.Busy -eq $true) {Start-Sleep -Seconds 3;}

$usernamefield = $ie.document.getElementByID('ysi_email')
$usernamefield.value = $username

$passwordfield = $ie.document.getElementByID('ysi_password')
$passwordfield.value = $password

$Link = $ie.document.getElementByID('ysi_btn_login')
$Link.click()

我似乎无法理解这里的问题,我已经查看了stackoverflow中的其他示例,但我仍然无法找到问题。

在python脚本的另一个例子中,相同的id工作正常。

这是截屏Edit: Providing screenshot of the elements

4 个答案:

答案 0 :(得分:4)

问题来自于在'ysi_email'加载的文档的DOM中找不到ID 'ysi_password''ysi_btn_login'https://website.com/login的对象。

要解决您在Chrome或Firerfox或资源管理器中加载文档并启动开发工具(按F12)并检查您想要查找的对象的问题。

enter image description here

根据您的意见,这是一个有效的解决方案:

$ie = New-Object -ComObject 'internetExplorer.Application'
$ie.Visible= $true # Make it visible

$username="test@toto.fr"

$password="test1"

$ie.Navigate("https://To your detailled URL")

While ($ie.Busy -eq $true) {Start-Sleep -Seconds 3;}

$usernamefield = $ie.document.getElementByID('ysi_email')
$usernamefield.value = "$username"

$passwordfield = $ie.document.getElementByID('ysi_password')
$passwordfield.value = "$password"

$Link = $ie.document.getElementByID('ysi_btn_login')
$Link.click()

$ie.Quit()

在这里你可以看到我的结果。

enter image description here

enter image description here

答案 1 :(得分:0)

我在IE11和本机COM方法上遇到了这个问题,我遇到了同样的问题,肯定存在某种错误,但是使用IE11的Selenium为我工作了,只需要导入.dll和IE驱动程序,其语法非常相似,足以让我了解它并获得所需。出于任何原因,硒都找到了元素,而本机IE COM却没有找到。另外,您可以使用Chrome或Firefox。

示例代码:

#Load Selenium to download all URL xml sites
Add-Type -Path "C:\Temp\WebDriver.dll" # Load the Selenium .Net library
$env:PATH += ";C:\Temp" # Load the IEDriverServer.exe from a path where it is stored
$ie_object = New-Object "OpenQA.Selenium.IE.InternetExplorerDriver" # Instantiate Internet Explorer

# Using Element ID for this example, but I prefer using The Name element.
$ie_object.Navigate().GoToURL( "https://website.com/login") # Navigate to login page URL
$InputUser = $ie_object.FindElementById("ysi_email") # Find the ID element for username input textbox, gt this from the DOM developer Tools search
$InputUser.SendKeys("usernamegoeshere") # Push the text
$PasswordUser = $ie_object.FindElementById("ysi_password") # Find the ID element for password input textbox, get this from the DOM developer Tools search
$PasswordUser.SendKeys("passwordgoeshere") # Push the text
$LoginButton = $ie_object.FindElementById("ysi_btn_login") # Find the ID element for the submit button, get this from the DOM developer Tools search
$LoginButton.Click() #Sent click to the submit button

https://www.seleniumhq.org/download/

获取硒

感谢newspaint,更多信息: https://newspaint.wordpress.com/2017/03/23/using-powershell-2-0-with-selenium-to-automate-internet-explorer-firefox-and-chrome/

答案 2 :(得分:0)

我通过从powershell调用Kantu selenium ide via command line解决了类似的Web自动化任务:

实际登录是通过录制的kantu硒脚本(以下代码中的“ do_login_macro.html”)完成的。

在我的情况下,登录凭据存储在宏中。但是您也可以使用cmd_var1 command line option从Powershell发送它们。

#max time allowed for macro to complete (change this value of your macros takes longer to run)
$timeout_seconds = 60  

#Launch Kantu for Chrome via command line
& "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" "file:///D:/test/do_login_macro.html?direct=1&close=1&savelog=log1.txt" 

$i = 0
#Loop: Wait for macro to complete => Wait for log file to appear in download folder
while (!(Test-Path "D:/test/log1.txt") -and ($i -lt $timeout_seconds)) 
{ 
    Write-Host  "Waiting for macro to finish, seconds=" $i
    Start-Sleep 1
    $i = $i + 1 
}


#Macro done - or timeout exceeded:
if ($i -lt $timeout_seconds)
{

    #Read first line of log file
    $s = Get-Content "D:/test/log1.txt" -First 1

    #Check if macro completed OK or not
    If ($s -contains "Status=OK")
    {
        Write-Host "Logged in!" $s
    }
    else
    {
        Write-Host  "Macro had an error:" $s
    }
    remove-item "D:/test/log1.txt"
}
else
{
    Write-Host  "Macro did not complete within the time given."
    #Cleanup => Kill Chrome instance 
    taskkill /F /IM chrome.exe /T 
}

答案 3 :(得分:0)

我正在使用PS 5.1和Edge浏览器,并且由于$Link对象不包含Click方法,因此上述解决方案无法完全正常工作。但是,使用Submit()方法确实有效。因此,就我而言,基本上我必须将倒数第二行更改为$Link.Submit()