Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

scripting - Auto Login to a website using powershell

enter image description hereI am getting following error message when i am running my powershell script

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`

My script:

$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()

I cant seem to understand the problem here, i have looked into other examples within stackoverflow but i still cant find the problem.

The same id works fine in another example in a python script.

Here is a screenshot Edit: Providing screenshot of the elements

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The problem comes from the fact that object of IDs 'ysi_email', 'ysi_password' and 'ysi_btn_login' are not found in the DOM of the document loaded at https://website.com/login.

To solve your trouble load your document in Chrome, or Firerfox or Explorer with the developpers tools activated (Press F12) and inspect the objects you want to find.

enter image description here


Here is a working solution according to your comments :

$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()

Here you can see the result for me.

enter image description here


enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...