web-dev-qa-db-fra.com

Connexion automatique à un site Web à l'aide de powershell

 enter image description here Je reçois un message d'erreur lorsque je lance mon script 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`

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

Je n'arrive pas à comprendre le problème ici, j'ai examiné d'autres exemples dans stackoverflow mais je n'arrive toujours pas à trouver le problème.

Le même identifiant fonctionne bien dans un autre exemple dans un script python.

Voici une capture d'écran  Edit: Providing screenshot of the elements

3
irish

Le problème provient du fait que les objets des ID 'ysi_email', 'ysi_password' et 'ysi_btn_login' ne sont pas trouvés dans le DOM du document chargé à https://website.com/login.

Pour résoudre votre problème, chargez votre document dans Chrome, Firerfox ou Explorer avec les outils de développement activés (appuyez sur F12) et inspect les objets que vous souhaitez rechercher.

 enter image description here


Voici une solution de travail en fonction de vos commentaires:

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

$username="[email protected]"

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

Ici vous pouvez voir le résultat pour moi.

 enter image description here


 enter image description here

2
JPBlanc

Je suis resté bloqué pendant un certain temps avec IE11 et les méthodes COM natives. Je rencontrais le même problème. Il devait y avoir une sorte de bogue, mais Selenium avec IE11 fonctionnait pour moi. Il suffisait d'importer le .dll et le IE pilote, la syntaxe était assez similaire pour que je puisse le maîtriser et obtenir ce dont j'avais besoin. Pour une raison quelconque, Selenium a trouvé les éléments alors que le IE COM natif ne l’a jamais fait. En outre, vous pouvez utiliser Chrome ou Firefox.

Exemple de code:

#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

Obtenez Selenium à partir de: https://www.seleniumhq.org/download/

Grâce à newspaint, plus d'infos: https://newspaint.wordpress.com/2017/03/23/using-powershell-2-0-with-Selenium-to-automate-internet-Explorer- firefox-and-chrome/

0
Maverick Sevmont

J'ai résolu une tâche similaire d'automatisation Web en appelant le Kantu Selenium ide via la ligne de commande à partir de powershell:

La connexion réelle est effectuée par un script kantu Selenium enregistré ("do_login_macro.html" dans le code ci-dessous).

Dans mon cas, les informations d'identification de connexion sont stockées dans la macro. Mais vous pouvez aussi les envoyer depuis Powershell avec l’option de ligne de commande cmd_var1 .

#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 
}
0
Tim Vanderzeil