none
[answered]AutoClic PowerShell RRS feed

  • Question

  • Hi everybody, 

    I will try speak english ^^

    I would like clic on the button for download the CSV file with PowerShell. ( The website it's Google Trends and URL file for download CSV expire in 1 day) 


    My code:

    $url = "https://trends.google.fr/trends/explore?date=now%201-H&q=technet"
    $ie = New-Object -com internetexplorer.application 
    $ie.visible = $true 
    $ie.navigate($url)
     
    while ($ie.Busy -eq $true) 
    { 
        Start-Sleep -Milliseconds 1000
    } 
    $ie.Document.getElementById("CSV").Click()

    Problems: 

    -I do not find the button ID

    -MESSAGE ERROR:

    Method invocation failed because [System.DBN
    At line:5 char:1
    + $ie.Document.getElementById("CSV").Click()
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidOperati
        + FullyQualifiedErrorId : MethodNotFound

    Thank you :) 




    • Edited by Atlante21 Friday, August 17, 2018 2:05 PM
    Tuesday, August 7, 2018 8:39 PM

All replies

  • $buttons = $ie.Document.body.getElementsByTagName("button") | Where-Object { $_.title -eq "CSV" }

    I can see there are 4 different buttons that can export CSV on the URL you provided. Depending on which one you want to click:

    $buttons[0].Click()
    $buttons[1].Click()
    $buttons[2].Click()
    $buttons[3].Click()

    Also, you will have to handle saving the csv file, for instance like:

    $obj = New-Object -Com WScript.Shell
    $obj.AppActivate('technet - Explore - Google Trends - Internet Explorer')
    Start-Sleep -Seconds 1
    $obj.SendKeys('%{N}')
    $obj.SendKeys('{TAB}')
    $obj.SendKeys('{ENTER}')
    

    But, it would be much easier if you use the Developer Tools to capture the queries made to download the CSV file and replicate it with the Invoke-WebRequest PowerShell cmdlet. But you'll have to find out the parameters passed with the URL.

    Invoke-WebRequest -Uri "https://trends.google.fr/trends/api/widgetdata/multiline/csv?req={%22time%22%3A%222018-08-07T19%5C%5C%3A42%5C%5C%3A30%202018-08-07T20%5C%5C%3A42%5C%5C%3A30%22,%22resolution%22%3A%22MINUTE%22,%22
    locale%22%3A%22en-US%22,%22comparisonItem%22%3A[{%22geo%22%3A{},%22complexKeywordsRestriction%22%3A{%22keyword%22%3A[{%22type%22%3A%22BROAD%22,%22value%22%3A%22technet%22}]}}],%22requestOptions%22%3A{%22property%22%3A%22%22,%22backend%
    22%3A%22CM%22,%22category%22%3A0}}&token=APP6_UEAAAAAW2tVtlKPhenWmGKfjXl8j8rBuhO-DlJi&tz=-120" -OutFile C:\temp\stuff.csv

    Tuesday, August 7, 2018 9:12 PM
  • Ok thank you :) 
    Tuesday, August 7, 2018 9:24 PM
  • You're welcome. If your question has been answered, please don't forget to mark the thread as answered. It helps improve the chance of others with similar questions obtaining answers.

    Friday, August 17, 2018 12:09 PM