0

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
2
  • You can use the Inspect element by right clicking. You'll find that the copy translation button has no id: <div class="jfk-tooltip-contentId">Copy translation</div> Commented Mar 13, 2020 at 16:34
  • Yes, that's what I know, my problem is to find any useful keyword to activate it. Commented Mar 13, 2020 at 17:23

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

3 Comments

It's working, but not for elements, which I want. I changed my code $page.AllElements | Where{$_.TagName -eq 'DIV'} and it's showing only part of the div's.
But why are you looking for div, vs link, tag, id, name, button, etc., element? As we all know, div are not elements. What site are you trying to hit, so that we can really see what you are working with?
Because this thing probably is not button. When I'm use 'Inspect', it highlights this div, so I tried to search it for some href phrase with links or something similar. But now I suspect it's a script. So now I understand even less. :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.