0

I am trying to use Selenium in my Excel VBA code to scrape a page that requires a login to access. I haven't even gotten to the scraping part because my code fails at the login window. The site I am trying to access is part of ESPN's fantasy sports.

Based on feedback from the first version of my question, I have modified my code to select the correct iframe and include a timed loop (per Automation of iTunes connect VBA), but it still isn't working. Here is what I have so far:

Sub TestLoginToESPN2()

    Dim bot As New WebDriver
    Dim URL As String
    Dim t As Date
    Dim ele As Object
    
    Const WAIT_TIME_SECS As Long = 30
    t = Timer
    URL = "https://fantasy.espn.com/football/team?leagueId=228988&teamId=10"
    
    With bot
        .Start "Chrome"
        .Get URL
        Do
            DoEvents
            If Timer - t > WAIT_TIME_SECS Then Exit Do
            On Error Resume Next
            .SwitchToFrame "disneyid-iframe"
            Set ele = .FindElementByCss("input.ng-valid-pattern")
            On Error GoTo 0
        Loop While ele Is Nothing
        If ele Is Nothing Then Exit Sub
        ele.SendKeys ("asfdasfd")
        
        Do
            DoEvents
            If Timer - t > WAIT_TIME_SECS Then Exit Do
            On Error Resume Next
            .SwitchToFrame "disneyid-iframe"
            Set ele = .FindElementByCss("input.ng-valid-parse")
            On Error GoTo 0
        Loop While ele Is Nothing
        If ele Is Nothing Then Exit Sub
        ele.SendKeys ("password123")
        
        .FindElementByCss("button.btn").Click
        
    End With
    
    Application.Wait Now + TimeValue("00:00:20")

End Sub

Only the first 1 or 2 characters of the username are entered in the User Name box, the complete password is entered but also in the User Name box (instead of the Password box) and I don't know if the button click works because the password is blank.

Any pointers on how to fix my code? Thanks!

gurs
  • 19
  • 6
  • 1
    Sorry but I'm not installing Selenium. I'm going to copy Mathieu's comment and ask, have you looked into using the [ESPN API](http://www.espn.com/apis/devcenter/overview.html)? – Profex Nov 05 '20 at 04:29
  • Thanks for the suggestion @Profex, but unfortunately ESPN is no longer issuing developer keys, so API access is closed. See http://www.espn.com/apis/devcenter/blog/read/publicretirement.html. – gurs Nov 05 '20 at 14:40
  • I made some progress and am posting here in case it helps someone down the road. I was able to get this to work by making 2 changes to the code above. First, I referenced the password field by XPath instead of CSS (the xpath is /html/body/div[1]/div/div/section/section/form/section/div[2]/div/label/span[2]/input). Second... – gurs Nov 10 '20 at 00:11
  • ... , for some reason I had to break up the text entry into each box into two lines, the first with only the first character, and the second line with the rest. For example, if the password is "password123", then I need to use ele.SendKeys ("p") and then ele.SendKeys ("assword123"), one line right after the other. I have no idea why. I'm sure there is a better/cleaner way to do this, but the above worked for me so I'm moving on! – gurs Nov 10 '20 at 00:11

1 Answers1

0

Here is the inartful but working code:

Sub TestLoginToESPN()

Dim bot As New WebDriver
Dim URL As String
Dim t As Date
Dim ele As Object

Const WAIT_TIME_SECS As Long = 30
t = Timer
URL = "https://fantasy.espn.com/football/team?leagueId=228988&teamId=10"

'Login to page
With bot
    .Start "Chrome"
    .Get URL
    .SwitchToFrame "disneyid-iframe"
    Do
        DoEvents
        If Timer - t > WAIT_TIME_SECS Then Exit Do
        On Error Resume Next
        Set ele = .FindElementByCss("input.ng-valid-pattern")
        On Error GoTo 0
    Loop While ele Is Nothing
    If ele Is Nothing Then Exit Sub
    With ele
        .SendKeys ("u")
        .SendKeys ("sername")
    End With
    
    Set ele = .FindElementByXPath("/html/body/div[1]/div/div/section/section/form/section/div[2]/div/label/span[2]/input")
    With ele
        .SendKeys ("p")
        .SendKeys ("assword123")
    End With
    
    .FindElementByCss("button.btn").Click 'click enter
End With

Application.Wait Now + TimeValue("00:00:20")

End Sub

gurs
  • 19
  • 6