Jump to content

Chrome webdriver download file


Recommended Posts

WARNING: page is full of ads.

I need to download a file (subtitle - download button at bottom of the page)

None of the clicks is working.

If I manually click, after program finished runnning, it downloads the file OK.

I have tried several methods of finding Elements.

IMPORTANT PART is marked in the script.

Thank you.

#include <Array.au3>
#include <Math.au3>
#include "wd_core.au3"
#include "wd_helper.au3"
#include <GuiComboBoxEx.au3>
#include <GUIConstantsEx.au3>
#include <ButtonConstants.au3>
#include <WindowsConstants.au3>

Opt('TrayIconDebug', 1)

Local $sDesiredCapabilities, $iIndex, $sSession
Local $nMsg, $lProcess = False

Dim Const $maxOffset = 5 ; Change according to how far you want to search; a bigger value means a slower comparison

Local $aBrowsers[][2] = [["Firefox", SetupGecko], _
                        ["Chrome", SetupChrome], _
                        ["Edge", SetupEdge]]

; Execute browser setup routine for user's browser selection
Call($aBrowsers[1][1])

Local $aDebugLevel[][2] = [["None", $_WD_DEBUG_None], _
                            ["Error", $_WD_DEBUG_Error], _
                            ["Full", $_WD_DEBUG_Info]]

; Set debug level
$_WD_DEBUG = $aDebugLevel[2][1]


_WD_Startup()

If @error <> $_WD_ERROR_Success Then
    Exit -1
EndIf

$sSession = _WD_CreateSession($sDesiredCapabilities)

;;;;;;;;  >>>>>>>>>>> THIS IS THE IMPORTANT PART
    _WD_Navigate($sSession, "https://www.subtitlist.com/subtitle/law-and-order-organized-crime-second-season/english/2603949")
    Sleep(333)
;~  $sElement = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, "//*[contains(text(),'Download') and contains(text(),'Subtitle')]", Default, True)
;~  $sElement = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, "//*[contains(text(),'Download')]", Default, True)
    $sElement = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, "//*[@href]", Default, True)
;~  $sElement = _WD_ExecuteScript($sSession, "document.getElementsByTagName('a');")
;~  _ArrayDisplay($sElement)
    if UBound($sElement)>0 Then
        For $i = 0 to UBound($sElement)-1
            _WD_ElementAction($sSession, $sElement[$i], "click")
            Sleep(333)
        Next
    EndIf

    Sleep(1000)
;;;;;;;;  >>>>>>>>>>> END OF IMPORTANT PART


;~_WD_DeleteSession($sSession)
;~_WD_Shutdown()

;~Exit

Func SetupGecko()
_WD_Option('Driver', 'geckodriver.exe')
_WD_Option('DriverParams', '--log trace')
_WD_Option('Port', 4444)

$sDesiredCapabilities = '{"capabilities": {"alwaysMatch": {"browserName": "firefox", "acceptInsecureCerts":true}}}'
EndFunc

Func SetupChrome()
_WD_Option('Driver', 'chromedriver.exe')
_WD_Option('Port', 9515)
_WD_Option('DriverParams', '--verbose --log-path="' & @ScriptDir & '\chrome.log"')

$sDesiredCapabilities = '{"capabilities": {"alwaysMatch": {"goog:chromeOptions": {"w3c": true, "excludeSwitches": [ "enable-automation"], "useAutomationExtension": false }}}}'
EndFunc

Func SetupEdge()
_WD_Option('Driver', 'msedgedriver.exe')
_WD_Option('Port', 9515)
_WD_Option('DriverParams', '--verbose')

$sDesiredCapabilities = '{"capabilities": {"alwaysMatch": {"ms:edgeOptions": {"binary": "' & StringReplace (@ProgramFilesDir, "\", "/") & '/Microsoft/Edge/Application/msedge.exe", "excludeSwitches": [ "enable-automation"], "useAutomationExtension": false}}}}'
EndFunc

 

Link to comment
Share on other sites

Have you looked into using _WD_UpdateDriver for this? That's what it's designed to do.

P.S. Why are you downloading chromedriver from a third party site? ☹️

Edit: I just realized that your thread title may have confused me. I'm guessing that you aren't downloading the webdriver. 🙄

Edited by Danp2
Link to comment
Share on other sites

_WD_LinkClickByText is working !!

I still have one small problem, but let's hope I find a solution for that too: the text is different. The 'English' part changes.

And... found something very useful!!!

$s01 = _WD_GetSource($sSession)

Thank you very very much !!

Link to comment
Share on other sites

What if there are multiple (maybe 6) buttons?!

And the one is not always in the same position (index) ?

<a type="button" href="https://www.subtitlist.com/download/dc2027d3b6508683050307acd9e4023a17438ee80d8a7787d9852e89062dd590b48f630a4a1f60879ccb5cc0440c847a98af8c1a317209686b9876d1f4c946c6" class="btn btn-outline-success mb-4"><i class="fa fa-download" aria-hidden="true"></i>
Download English Subtitle</a>

How do I combine the two?

"//*[contains(text(),'Download') and contains(text(),'Subtitle')]"

and

"//a[@type='button']"

Like this?

"//a[@type='button']//*[contains(text(),'Download') and contains(text(),'Subtitle')]"

I will test myself in a few minutes.

 

Edit: hmmmm, it looks like there's one single button with tag 'a' and type='button' - the exact Download button I need!!

So your suggestions works perfectly

Edited by queensoft
Link to comment
Share on other sites

29 minutes ago, queensoft said:

What if there are multiple (maybe 6) buttons?!

Those aren't buttons. They are actually links (A elements) that are coded to appear as buttons. The xref I posted was correct for the webpage you previously listed. If this isn't valid for other pages, then you can add additional criteria, like this --

$sElement = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, "//a[@type='button' and contains(text(),'Download') and contains(text(),'Subtitle')]")

 

Link to comment
Share on other sites

Another question: how do I set a different download directory for multiple webdriver instances?

Right now it uses the Windows defaut download directory.

I have managed to start multiple webdriver instances, using different profile folders:

; extract string from command line parameter - that means different folders for different instances
Global $user_profile = 'D:\\Diverse\\' & StringReplace('2021_10_25_09_37_06.txt', '.txt', '')

; set profile folder
$sDesiredCapabilities = '{"capabilities": {"alwaysMatch": {"goog:chromeOptions": {"w3c": true, "excludeSwitches": [ "enable-automation"], "useAutomationExtension": false, "args":["user-data-dir=' & $user_profile & '"] }}}}'

I need each instance to download to a different folder.

 

FOUND SOLUTION - also from Danp2 !!!

$sDesiredCapabilities = '{"capabilities": {"alwaysMatch": {"goog:chromeOptions": {"w3c": true, "excludeSwitches": [ "enable-automation"], "useAutomationExtension": false, "args":["user-data-dir=' & $user_profile & '"], "prefs": {"download.default_directory": "e:\\"} }}}}'

 

Edited by queensoft
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...