Skip to content

Retrieve WebDriver URL at a precise moment. #233

Description

@kjourdan1

Hi,
I'm using the Selenium PowerShell Module to automate HP Warranty check because HP doesn't provide an API like all other manufacturers.

I would like to retrieve the URL of the webdriver once it has checked the warranty.
Here's the function in my script doing the warranty check :
function CheckWarranty {
param(
[Parameter(Mandatory=$true)]
[string]$EdgeDriverPath,
[Parameter(Mandatory=$true)]
[string]$SerialNumber
)

# Environnement Selenium
Import-Module "$($EdgeDriverPath)\WebDriver.dll"

# Créer un objet EdgeOptions pour configurer le navigateur Edge
#$EdgeOptions.AcceptInsecureCertificates = $True

# Créer l'objet du pilote du navigateur Edge
$EdgeDriver = New-Object OpenQA.Selenium.Edge.EdgeDriver -ArgumentList $EdgeDriverPath

# Définir l'URL de la page de vérification de la garantie HP
$warrantyCheckURL = "https://support.hp.com/fr-fr/checkwarranty"

# Vérifier la garantie HP avec Selenium
$EdgeDriver.Url = $warrantyCheckURL

Write-Host "Vérification de la garantie HP..."
Start-Sleep -Seconds 3

$acceptButton = $null
try {
    $acceptButton = $EdgeDriver.FindElementById('onetrust-accept-btn-handler')
}
catch {
    # Fermer le navigateur si le bouton est introuvable
    Write-Host "Le bouton 'Accept Cookies' est introuvable."
    $EdgeDriver.Quit()
    throw
}

# Vérifier si le bouton est visible, cliquable et non obstrué
if ($acceptButton.Displayed -and $acceptButton.Enabled) {
    $acceptButton.Click()
}
else {
    # Fermer le navigateur si le bouton n'est pas clickable
    Write-Host "Le bouton 'Accept Cookies' n'est pas clickable."
    $EdgeDriver.Quit()
    throw
}

$EdgeDriver.FindElementById("inputtextpfinder").SendKeys("$($SerialNumber)")
$EdgeDriver.FindElementById("FindMyProduct").Click()
Start-Sleep -Seconds 3
**$WarrantyURL = $EdgeDriver.CurrentWindowHandle()**

 try {
    $DebutGarantie = $EdgeDriver.FindElementByXPath('//*[@id="directionTracker"]/app-layout/app-check-warranty/div/div/div[2]/app-warranty-details/div/div[2]/section/app-left-panel/div/div[5]/div[1]/div[2]').Text
    $FinGarantie = $EdgeDriver.FindElementByXPath('//*[@id="directionTracker"]/app-layout/app-check-warranty/div/div/div[2]/app-warranty-details/div/div[2]/section/app-left-panel/div/div[5]/div[3]/div[2]').Text
    $FinGarantie = $FinGarantie.Replace("Date de fin", "").Trim()
    #Write-Host "La garantie a commencé le $DebutGarantie et s'est terminée le $FinGarantie"
}
catch {
    Write-Host "Les dates de garanties ne sont pas à l'emplacement principal, tentative sur un autre emplacement..." -ForegroundColor Yellow
    }
    try {
        # Remplacez ces sélecteurs XPath par ceux correspondant aux autres emplacements où les dates peuvent se trouver
        $DebutGarantie = $EdgeDriver.FindElementByXPath('//*[@id="directionTracker"]/app-layout/app-check-warranty/div/div/div[2]/app-warranty-details/div/div[2]/main/div[4]/div/div[2]/div/div/div[1]/div[4]/div[2]').Text
        $FinGarantie = $EdgeDriver.FindElementByXPath('//*[@id="directionTracker"]/app-layout/app-check-warranty/div/div/div[2]/app-warranty-details/div/div[2]/main/div[4]/div/div[2]/div/div/div[1]/div[5]').Text
        $FinGarantie = $FinGarantie.Replace("Date de fin", "").Trim()
        #Write-Host "La garantie a commencé le $DebutGarantie et s'est terminée le $FinGarantie"
    }
    catch {
        # Fermer le navigateur si la garantie ne peut pas être récupérée"
        $EdgeDriver.Quit()
        Start-Sleep -Seconds 1
    }
finally {
    if ($null -eq $DebutGarantie -or $null -eq $FinGarantie) {
        Write-Host "Impossible de récupérer les dates de début et de fin de la garantie." -ForegroundColor Red
    throw
    }
    else {
        Write-Host "La garantie a commencé le $DebutGarantie et s'est terminée le $FinGarantie"
    }
}


# Fermer le navigateur
$EdgeDriver.Quit()

return @{
    'DebutGarantie' = $DebutGarantie
    'FinGarantie' = $FinGarantie
}

}

As you can see I'm trying with the CurrentWindowsHandle but if you have any suggestions I'm curious to know them !
Thank You

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions