Search code examples
internet-explorertwittercomautohotkey

twitter login trough IE via COM by Autohotkey


I want to login into a twitter account https://twitter.com/login using internet explorer via COM with autohotkey, but i cant set the value of the input fields and i dont understand why.

Code

Prepare (sleep instead of more complex waiting for easier reading):

wb := ComObjCreate("InternetExplorer.Application")
sleep 500

wb.Navigate("https://twitter.com/login")
sleep 500

wb.visible := true   ;only for debugging

Code which does nothing:

wb.document.querySelector(".js-username-field").value := "Test"
wb.document.querySelector(".js-password-field").value := "TestPass"

Problem

This code works fine on www.google.com or the search on www.stackoverflow.com (with the right selectors). But on Twitter it does not insert the values. I would really appreciate it, if someone could help me figure out why.


Remark

It can probably done with a POST request on a "WinHttp.WinHttpRequest.5.1" object, but i am not very experienced with this and would prefer to work with the IE object.


Solution

  • In the document of the Twitter login page, there are two elements respectively, that match your selectors, and both pairs (username and password fields) basically look the same in terms of HTML:

    enter image description here

    Since querySelector() will only select the first matching element in DOM order, it will select the inputs you don't want.
    To solve this, you could use querySelectorAll() and access the desired elements by index (which I don't recommend for several reasons, e.g. some IE versions don't support it).
    I suggest adjusting the selector in order to explicitly select the desired inputs. One way to do is, is by including one or several parents in the selector path:

    wb.document.querySelector("div.clearfix .js-username-field").value := "Test"
    wb.document.querySelector("div.clearfix .js-password-field").value := "TestPass"
    

    [Edit] Alternative selector:

    #page-container .js-username-field
    #page-container .js-password-field
    

    In encourage you to test out better ways to distinctly address the inputs, maybe the containing form or some other elements are a better fit. If you choose to go with another selector, please let me know and I'll update this answer with your alternative.