PDA

View Full Version : Gmail Login failed (Web Scraping)



syed_iqbal
03-11-2017, 12:47 PM
Hi,

I'm new to web scraping. Now I trying to login into Gmail. After entering the password, the code is doing nothing (not opening Gmail account). Pls go through it and correct it.


Dim MyBrowser As InternetExplorer
Dim HTMLDoc As MSHTML.HTMLDocument
Dim HTMLInput As MSHTML.IHTMLElement
Dim HTMLButtons As MSHTML.IHTMLElementCollection
Dim HTMLButton As MSHTML.IHTMLElement

Sub MyGmail()
Dim MyHTML_Element As IHTMLElement
Dim MyURL As String
On Error GoTo Err_Clear
MyURL = "https://www.gmail.com"
Set MyBrowser = New InternetExplorer
MyBrowser.Silent = True
MyBrowser.navigate MyURL
MyBrowser.Visible = True
Do While MyBrowser.ReadyState <> READYSTATE_COMPLETE
Loop
Set HTMLDoc = MyBrowser.Document
Set HTMLInput = HTMLDoc.getElementById("Email")
HTMLInput.Value = "learningexcelvba@gmail.com"
Set HTMLButtons = HTMLDoc.getElementsByName("next")
HTMLButtons(0).Click
Set HTMLDoc = MyBrowser.Document
Set HTMLInput = HTMLDoc.getElementById("Passwd-hidden")
HTMLInput.Value = "excelvba"
Set HTMLButtons = HTMLDoc.getElementsById("signIn")
HTMLButtons(0).Click
Err_Clear:
If Err <> 0 Then
Err.Clear
Resume Next[/COLOR]
End If
End Sub

Thank you in advance.

regards
shiva

ashleyuk1984
03-11-2017, 03:12 PM
If your signing in for the first time, then this works.


Dim MyBrowser As InternetExplorer
Dim HTMLDoc As MSHTML.HTMLDocument
Dim HTMLInput As MSHTML.IHTMLElement
Dim HTMLButtons As MSHTML.IHTMLElementCollection
Dim HTMLButton As MSHTML.IHTMLElement
Sub MyGmail()


Dim MyHTML_Element As IHTMLElement
Dim MyURL As String


On Error GoTo Err_Clear


MyURL = "https://accounts.google.com/ServiceLogin"
Set MyBrowser = New InternetExplorer
MyBrowser.Silent = True
MyBrowser.navigate MyURL
MyBrowser.Visible = True


Do While MyBrowser.ReadyState <> READYSTATE_COMPLETE
Loop


Set HTMLDoc = MyBrowser.Document
Set HTMLInput = HTMLDoc.getElementById("Email")


HTMLInput.Value = "learningexcelvba@gmail.com"


Set HTMLButtons = HTMLDoc.getElementsByName("next")


HTMLButtons(0).Click


Set HTMLDoc = MyBrowser.Document
Set HTMLInput = HTMLDoc.getElementById("Passwd")


HTMLInput.Value = "excelvba"


Set HTMLButtons = HTMLDoc.getElementsById("signIn")


HTMLButtons(0).Click


Err_Clear:


If Err <> 0 Then
Err.Clear
Resume Next
End If


End Sub



However the second time, when your email is already in the predetermined field, it acts a little bit differently.
But hopefully this should get you going.

I only changed two things, the URL, and your password element name.

syed_iqbal
03-11-2017, 03:31 PM
Hi ashleyuk (http://www.vbaexpress.com/forum/member.php?49500-ashleyuk1984), thank you for reply. But , the code is not taking the password. pls check in the browser.

ashleyuk1984
03-11-2017, 03:59 PM
There must be something different between our two machines, because the code is working perfectly fine on my end.
Like I said above, it works for a "first time" log in... But the second time when the email address is already predetermined by the browser, the code doesn't work.
I did state this above, and that's probably what your experiencing.

syed_iqbal
03-11-2017, 04:25 PM
Hi, i run the code after removing mail id from login mail. but below two lines of code not executing. if the above code is working on your machine, then why below two lines of code is not working on my system. i can see hidden password (in the form of dots) but code is not clicking on the signin button. My excel version is 2016 (trial version). I think there is no issue with the trial version. bcoz i executed few other programs successfully.
Set HTMLButtons = HTMLDoc.getElementsByid("signIn")
HTMLButtons(0).Click

why above code is not working.

Ravi494
03-20-2017, 11:39 PM
Set HTMLButtons = HTMLDoc.forms(0)
HTMLButtons(0).submit

OR

Mybrowser.Document.forms(0).submit



its type is (Submit) so this is how it works. I am new to vba so I cant explain this but I know this.

Ravi494
03-21-2017, 12:08 AM
Hi
Can someone please explaine me in below code in the line
'ie.Document. getelementbyid("q") .Value = "VBA Express""

how it works, i mean "q" is not the "id" its the "name" and the "id" is "lst-ib" but it doesnt work if i write
'ie.Document. getelementbyname("q"). Value = "VBA Express"
'ie.Document. getelementbyid("lst-ib"). Value = "VBA Express"

its working how it should not be working and this is making me crazy

(i am new to vba and also to any online forum ever)