无法使用 PowerShell 登录抽搐
Can't login to twitch using PowerShell
我正在编写 PowerShell 脚本以通过 Internet Explorer 登录到 twitch。我已成功填充用户名和密码字段,但登录按钮仍处于禁用状态。需要启用登录按钮并单击以成功登录。请指导我完成这项工作。下面是我的脚本
$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
输入框似乎实际上并未接受这些值。除了这个问题,还有两个登录按钮。一个是您看到的“已禁用”,另一个是在输入框被填满时显示的。以下命令显示每个 css 的差异。
$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>
如果我们使用 Selenium Powershell 模块,它会更容易一些,尽管对 Internet Explorer 的要求使它更具挑战性。以下代码可用于使用 Internet Explorer 成功登录 Twitch。
首先,要使驱动程序正常工作,IE 中的所有安全区域都必须打开或关闭保护模式,它们必须匹配。此代码将为除本地计算机之外的所有区域禁用保护模式。
1..4 | foreach {
Set-ItemProperty "hkcu:\Software\Microsoft\Windows\CurrentVersion\Internet Settings\zones$_\" -Name 2500 -Value 3
}
现在我们可以自动登录了。通过标签查找按钮的问题在于,在 IE 中只有 returns 一个按钮。通过使用 xpath,我们能够找到所有按钮并按文本进行过滤。
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()
我仍然建议使用 Chrome、Firefox 或 New Edge 而不是 IE。以下代码将在 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()
我正在编写 PowerShell 脚本以通过 Internet Explorer 登录到 twitch。我已成功填充用户名和密码字段,但登录按钮仍处于禁用状态。需要启用登录按钮并单击以成功登录。请指导我完成这项工作。下面是我的脚本
$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
输入框似乎实际上并未接受这些值。除了这个问题,还有两个登录按钮。一个是您看到的“已禁用”,另一个是在输入框被填满时显示的。以下命令显示每个 css 的差异。
$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>
如果我们使用 Selenium Powershell 模块,它会更容易一些,尽管对 Internet Explorer 的要求使它更具挑战性。以下代码可用于使用 Internet Explorer 成功登录 Twitch。
首先,要使驱动程序正常工作,IE 中的所有安全区域都必须打开或关闭保护模式,它们必须匹配。此代码将为除本地计算机之外的所有区域禁用保护模式。
1..4 | foreach {
Set-ItemProperty "hkcu:\Software\Microsoft\Windows\CurrentVersion\Internet Settings\zones$_\" -Name 2500 -Value 3
}
现在我们可以自动登录了。通过标签查找按钮的问题在于,在 IE 中只有 returns 一个按钮。通过使用 xpath,我们能够找到所有按钮并按文本进行过滤。
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()
我仍然建议使用 Chrome、Firefox 或 New Edge 而不是 IE。以下代码将在 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()