+ Reply to Thread
Results 1 to 17 of 17

Issue with Selenium and driver as a variable?

Hybrid View

  1. #1
    Registered User
    Join Date
    09-07-2015
    Location
    2871
    MS-Off Ver
    10
    Posts
    9

    Issue with Selenium and driver as a variable?

    Hi all,

    First post here so go easy on me please!

    I'm using Selenium VBA to automate Firefox. The site i'm scraping has a limit on the loads per session, so i want to create a number of broswer's and cycle through each in an attempt to manage the load and prevent everything freezing. Anyway, onto the question.

    This doesn't work

    ' Set Up Selenium
    Dim A As New SeleniumWrapper.WebDriver
    Dim B As New SeleniumWrapper.WebDriver
    Dim C As New SeleniumWrapper.WebDriver
    Dim D As New SeleniumWrapper.WebDriver
    Dim E As New SeleniumWrapper.WebDriver
    
    Sub initiate_browsers()
    
        ' Number of browsers to create
        Max_Row = Application.WorksheetFunction.CountA(ParameterSheet.Range("E3:E7")) + 2
           
        ' Loop through each
        For i = 3 To Max_Row
            B_variable = ParameterSheet.Range("E" & i).Value
            Call open_browser(Bro_Var:=B_variable)
        Next i
    
    End Sub
    
    ' Create Browser
    Sub open_browser(ByVal Bro_Var As SeleniumWrapper.WebDriver)
    
        Bro_Var.Start "firefox", "https://www.google.com/"
        Bro_Var.Open "/"
    
    End Sub
    It fails where i pass the variable name ("A") into the second sub where i plan to use it as a variable. Not a value...

    stumped?!?!

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,646

    Re: Issue with Selenium and driver as a variable?

    I'm sorry but that code doesn't look right.

    Forone thing, B_variable isn't declared anywhere.

    Also, the Sub open_browser requires a SeleniumWrapper.WebDriver object to be passed to it.
    If posting code please use code tags, see here.

  3. #3
    Forum Expert skywriter's Avatar
    Join Date
    06-09-2014
    Location
    USA
    MS-Off Ver
    365 Version 2505
    Posts
    2,790

    Re: Issue with Selenium and driver as a variable?

    I don't claim to know anything about this code but a couple of things jump out at me. You are declaring:
    Dim A As New SeleniumWrapper.WebDriver
    Dim B As New SeleniumWrapper.WebDriver
    Dim C As New SeleniumWrapper.WebDriver
    Dim D As New SeleniumWrapper.WebDriver
    Dim E As New SeleniumWrapper.WebDriver
    all these variables as SeleniumWrapper.WebDriver

    Then you have:
     B_variable
    which I don't see you declaring anywhere.
    You then pass that variable to the open_browser sub which seems to need a SeleniumWrapper, but you do it in a very, at least to me unusual way.
     Call open_browser(Bro_Var:=B_variable)
    Why does it have to be this way?
    Why can't it be?:
    Call open_browser(B_variable)
    I don't know what a SeleniumWrapper.WebDriver is, but does this create one?
    B_variable = ParameterSheet.Range("E" & i).Value
    I know what drivers are and it seems strange that you could create one in a worksheet unless it is created through text somehow. Like I said, I don't claim to understand the code.
    Last edited by skywriter; 09-07-2015 at 02:28 PM.

  4. #4
    Registered User
    Join Date
    09-07-2015
    Location
    2871
    MS-Off Ver
    10
    Posts
    9

    Re: Issue with Selenium and driver as a variable?

    Thanks for the replies. The code i posted wasn't the cleanest. Let me try and explain myself better.

    The code here works and is the normal way of opening a browser using selenium

    Dim A As New SeleniumWrapper.WebDriver
    A.Start "firefox", "https://www.google.com/"
    A.Open "/"
    But. I want to put this in a loop passing in what selenium often calls the "driver" as a variable (in the last case "A").
    The code below attempts to do that but fails...

    ' Set Up Selenium
    Dim A As New SeleniumWrapper.WebDriver
    Dim B As New SeleniumWrapper.WebDriver
    Dim C As New SeleniumWrapper.WebDriver
    Dim D As New SeleniumWrapper.WebDriver
    Dim E As New SeleniumWrapper.WebDriver
    
    Sub initiate_browsers()
    
        Possible_Browsers = Array("A", "B", "C", "D", "E")
     
        'Loop
        For Each Item In Possible_Browsers
            Call open_browser(Item)
        Next
           
    End Sub
    
    ' Create Browser
    Sub open_browser(Bro_Var)
    
    '    A.Start "firefox", "https://www.google.com/"
    '    A.Open "/"
    
        Bro_Var.Start "firefox", "https://www.google.com/"
        Bro_Var.Open "/"
    
    End Sub
    Thanks again for the help

  5. #5
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,646

    Re: Issue with Selenium and driver as a variable?

    How does the code fail?

  6. #6
    Registered User
    Join Date
    09-07-2015
    Location
    2871
    MS-Off Ver
    10
    Posts
    9

    Re: Issue with Selenium and driver as a variable?

    Quote Originally Posted by Norie View Post
    How does the code fail?
    Run-Time error '424'
    Object required

    Its like its not recognizing the value "A" being passed as the variable declared?

  7. #7
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: Issue with Selenium and driver as a variable?

    When declaring your array, a, b, c and d shouldn't be in quotation marks

  8. #8
    Registered User
    Join Date
    09-07-2015
    Location
    2871
    MS-Off Ver
    10
    Posts
    9

    Re: Issue with Selenium and driver as a variable?

    Quote Originally Posted by Kyle123 View Post
    When declaring your array, a, b, c and d shouldn't be in quotation marks
    That doesn't appear to matter. The "items" are being passed to the second sub.
    I just checked with a msgbox.

  9. #9
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: Issue with Selenium and driver as a variable?

    Of course it matters. You are passing a string to the sub, not an instance of the web driver. You've declared a as a string in your array

  10. #10
    Registered User
    Join Date
    09-07-2015
    Location
    2871
    MS-Off Ver
    10
    Posts
    9

    Re: Issue with Selenium and driver as a variable?

    Yes that does work thanks!

    However, i'm back to my original problem. How can i pull that variable as text from a sheet and then pass that as an instance to the second sub?

    .......
    B_variable = ParameterSheet.Range("E" & i).Value
    call second_sub(B_variable)
    .......
    I want to be able to do this because, in the sheet i want to log the page loads for each browser.

  11. #11
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,646

    Re: Issue with Selenium and driver as a variable?

    What exactly do you have on the sheet?

  12. #12
    Registered User
    Join Date
    09-07-2015
    Location
    2871
    MS-Off Ver
    10
    Posts
    9

    Re: Issue with Selenium and driver as a variable?

    Just the variable name...
    Capture.PNG

  13. #13
    Forum Expert skywriter's Avatar
    Join Date
    06-09-2014
    Location
    USA
    MS-Off Ver
    365 Version 2505
    Posts
    2,790

    Re: Issue with Selenium and driver as a variable?

    What about:

    Sub open_browser(Bro_Var)
    
    Select Case Bro_Var
    
    Case Is = "A"
        A.Start "firefox", "https://www.google.com/"
        A.Open "/"
    
    Case Is = "B"
        B.Start "firefox", "https://www.google.com/"
        B.Open "/"
        
        ' you get the idea.
        
    End Select
    
    End Sub

  14. #14
    Registered User
    Join Date
    09-07-2015
    Location
    2871
    MS-Off Ver
    10
    Posts
    9

    Re: Issue with Selenium and driver as a variable?

    Quote Originally Posted by skywriter View Post
    What about:
    Ha, yes!
    That's what i'm doing now - but its not very elegant. Also there is going to be a lot of repeated/redundant code in each select...

    Hoping someone here can help with something better!

  15. #15
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,646

    Re: Issue with Selenium and driver as a variable?

    You can't take the values from the sheet and use them to refer to variables in the code.

  16. #16
    Registered User
    Join Date
    09-07-2015
    Location
    2871
    MS-Off Ver
    10
    Posts
    9

    Re: Issue with Selenium and driver as a variable?

    Quote Originally Posted by Norie View Post
    You can't take the values from the sheet and use them to refer to variables in the code.
    Even though they will always be the same?
    I'm not creating the variable from the sheet.

  17. #17
    Registered User
    Join Date
    09-07-2015
    Location
    2871
    MS-Off Ver
    10
    Posts
    9

    Re: Issue with Selenium and driver as a variable?

    Quote Originally Posted by Norie View Post
    You can't take the values from the sheet and use them to refer to variables in the code.
    OK thanks for the help

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. VBA and Selenium
    By cabroncito29 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-05-2015, 10:46 AM
  2. Using Selenium and Drop downs in Google Finance
    By JKlem in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-21-2014, 09:07 PM
  3. Need Help with Nested Errors and Selenium
    By BYizz in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-21-2014, 10:02 AM
  4. Using Selenium to get time and distance information from Google Maps
    By JKlem in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-04-2014, 05:23 PM
  5. [SOLVED] Lorry driver shifts (day,night,saturday, sunday) variable payrate
    By aportik in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 12-17-2013, 06:25 AM
  6. Macro issue: Object variable or with block variable not set error
    By utgjmb1 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 07-29-2013, 06:07 AM
  7. Replies: 1
    Last Post: 07-24-2013, 02:45 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1