Search code examples
powershellinternet-explorerbrowserautomationtwitch

Can't login to twitch using PowerShell


I'm working on a PowerShell script to login to twitch via Internet Explorer. I'm successful to populate Username and Password fields but Log In button remains disabled. Log In button needs to be enabled and click for successful login. Please guide me to make this work. Below is my script

$Url ="https://www.twitch.tv/login"
$ie = New-Object -ComObject 'internetExplorer.Application'
$ie.Visible= $true # Make it visible

$username="Your_User_Name"
$password="Your_Password"

$ie.Navigate($Url)

while($ie.Busy) { Start-Sleep -Seconds 2 }
while($ie.ReadyState -ne 4) {start-sleep -m 100} 

$usernamefield = $ie.Document.IHTMLDocument3_getElementById("login-username")  
$usernamefield.value = $username;

$passwordfield = $ie.Document.IHTMLDocument3_getElementById("password-input")
$passwordfield.value = $password;

$loginbutton = $ie.document.getElementsByTagName("button")[3].click()

As shown in screen shot, user name and password get populated in required fields but Log In button remains disabled after running script and also doesn't click to login


Solution

  • The input boxes appear to not actually be accepting the values. In addition to that issue, there are two login buttons. One is the "disabled" one you see and the other is shown when the input boxes are filled. The following command shows the difference in css for each.

    $ie.Document.getElementsByTagName("Button") | where {$_.textContent -eq "Log In"} | foreach {"Log In button`n";$_.outerhtml;"`n"}
    
    Log In button
    
    <button class="tw-block tw-c-text-inherit tw-full-height tw-full-width tw-interactive tw-pd-l-0 tw-pd-r-1 tw-tab-item" role="tab"><div class="tw-align-left tw-flex tw-flex-column tw-full-heig
    ht"><div class="tw-flex-grow-0"><div class="tw-font-size-5 tw-regular">Log In</div></div><div class="tw-flex-grow-1"></div><div class="tw-flex-grow-0"><div class="tw-tabs__active-indicator" d
    ata-test-selector="ACTIVE_TAB_INDICATOR"></div></div></div></button>
    
    
    Log In button
    
    <button disabled="" class="tw-align-items-center tw-align-middle tw-border-bottom-left-radius-medium tw-border-bottom-right-radius-medium tw-border-top-left-radius-medium tw-border-top-right-
    radius-medium tw-core-button tw-core-button--disabled tw-core-button--primary tw-full-width tw-inline-flex tw-interactive tw-justify-content-center tw-overflow-hidden tw-relative" data-a-targ
    et="passport-login-button"><div class="tw-align-items-center tw-core-button-label tw-flex tw-flex-grow-0"><div class="tw-flex-grow-0" data-a-target="tw-core-button-label-text">Log In</div></d
    iv></button>
    

    If we use the Selenium Powershell module, it makes it somewhat easier, though the requirement for Internet Explorer made it a bit more challenging. The following code can be used to successfully log into Twitch with Internet Explorer.

    First, for the driver to work all the security zones in IE must have protected mode on or off, they have to match. This code will disable protected mode for all zones except Local Computer.

    1..4 | foreach {
        Set-ItemProperty "hkcu:\Software\Microsoft\Windows\CurrentVersion\Internet Settings\zones\$_\" -Name 2500 -Value 3
    }
    

    Now we can automate the login. The issue with finding the button by tag, is that in IE that only returns one button. By using xpath we are able to find all buttons and filter by the text.

    Install-Module Selenium -Scope CurrentUser -Force
    
    $Url ="https://www.twitch.tv/login"
    $username="Your_User_Name"
    $password="Your_Password"
    
    $driver = Start-SeInternetExplorer -StartURL $Url
    
    $usernamefield = $driver.FindElementById("login-username")
    
    Send-SeKeys -Element $usernamefield -Keys $username
    
    $passwordfield = $driver.FindElementById("password-input")
    
    Send-SeKeys -Element $passwordfield -Keys $password
    
    $loginbutton = $driver.FindElementsByXPath("//button")| where text -eq 'Log In' | select -last 1
    
    $loginbutton.Click()
    

    I still recommend using Chrome, Firefox, or New Edge over IE. The following code will achieve the same result in Chrome.

    Install-Module Selenium -Scope CurrentUser -Force
    
    $Url ="https://www.twitch.tv/login"
    $username="Your_User_Name"
    $password="Your_Password"
    
    $driver = Start-SeChrome -StartURL $Url
    
    $usernamefield = Find-SeElement -Driver $driver -Id "login-username"
    
    Send-SeKeys -Element $usernamefield -Keys $username
    
    $passwordfield = Find-SeElement -Driver $driver -Id "password-input"
    
    Send-SeKeys -Element $passwordfield -Keys $password
    
    $loginbutton = Find-SeElement -Driver $driver -By TagName "button" | where text -eq 'Log In' | select -last 1
    
    $loginbutton.Click()