Paweł Stępniak
Paweł Stępniak

Reputation: 11

How to press button on web page by script in Powershell

I want to press 'copy translation' button on translate.google.com by script, but I can not find it ID or call it without ID.

I tried:

.parsedhtml.body.GetElementsByClassName()

.parsedhtml.body.GetElementsById()

but I have no bloody idea how to find proper phrase in such complex web page. (I'm new to powershell.) I found div in which this button is.

Current code:

$text= 'Some text to translate'
$URI = 'https://translate.google.pl/#en/pl/'

$page = Invoke-WebRequest -Uri $URI$text -method get

$selector= 'tlid-copy-translation-button copybutton jfk-button-flat source-or-target-footer-button right-positioned jfk-button-hover jfk-button-active jfk-button-focused jfk-button-jfk-button-clear-outline'

$page.ParsedHtml.body.getElementsByTagName('div') | 
Where {$_.getAttributeNode('class').Value -eq $selector}
$submitButton.click()

$text2= Get-Clipboard
Write-Output $text2

Upvotes: 0

Views: 1901

Answers (1)

postanote
postanote

Reputation: 16086

This is really straight forward, as long as you know the element needed. For example, See this StackOverflow Q&A discussion as it would be similar to what you are after and a bit more.

Populate username/password in webpage with Powershell

Navigate the site form elements, not the Internet Explorer elements

$url = 'https://pwpush.com'

($FormElements = Invoke-WebRequest -Uri $url -SessionVariable fe)   

<#
    StatusCode        : 200
    StatusDescription : OK
    Content           : <!DOCTYPE html>
                        <html>
...
#>



($Form = $FormElements.Forms[0]) | 
Format-List -Force

<#
    Id     : new_password
    Method : post
    Action : /p
    Fields : ...}
#>

$Form | 
Get-Member

<#
       TypeName: Microsoft.PowerShell.Commands.FormObject

    Name        MemberType   Definition                                                        
    ----        ----------   ----------                                                        
    Equals      Method       bool Equals(System.Object obj)                                    
    GetHashCode Method       int GetHashCode()                                                 
    GetType     Method       type GetType()                                                    
    ToString    Method       string ToString()                                                 
    Action      Property     string Action {get;}                                              
    Fields      Property     System.Collections.Generic.Dictionary[string,string] Fields {get;}
    Id          Property     string Id {get;}                                                  
    Method      Property     string Method {get;}                                              
    MSDN        ScriptMethod System.Object MSDN();
#>

$Form.Fields

<#
    Key                          Value                                                                                   
    ---                          -----                                                                                   
    utf8                         ✓                                                                                       
    authenticity_token           2mebmGbAJsseDW+/TeTBXAelq1s8kH5Zgb6W14Pxtba6CyWsAM4SfVqdJWdVmu5HjxIGUCWjEGhy6fLTB38UhA==
    password_payload             Enter the Password to be Shared                                                         
    password_expire_after_days   7                                                                                       
    password_expire_after_views  5                                                                                       
    password_deletable_by_viewer on                                                                                      
    commit                       Push it

# so you end up here

Clear-Host

$password = '1234'
$loginUrl = 'https://pwpush.com'

$ie = New-Object -com internetexplorer.application
$ie.visible = $true
$ie.navigate($loginUrl)

while ($ie.Busy -eq $true) { Start-Sleep -Seconds 1 }

($ie.document.getElementById('password_payload') | select -first 1).value = $password
Start-Sleep -Seconds 1 

$ie.Document.getElementsByName('commit').Item().Click();
Start-Sleep -Seconds 1

Upvotes: 1

Related Questions