简体   繁体   English

使用Selenium登录到ESPN

[英]Logging into ESPN using Selenium

I am trying to use Selenium to automate some tasks on ESPN and I first need to log into my account since I get redirected to the login page when I try to access an ESPN page. 我正在尝试使用Selenium来自动执行ESPN上的某些任务,由于要在访问ESPN页面时重定向到登录页面,因此我首先需要登录到我的帐户。 Here's the login form: 这是登录表单:

<form name="loginForm" method="post" action="https://r.espn.go.com/espn/memberservices/pc/login">
<input type="hidden" name="failedAttempts" value="2">
<input type="hidden" name="SUBMIT" value="1">
<input type="hidden" name="failedLocation" value="http://games.espn.go.com/ffl/signin?redir=http%3A%2F%2Fgames.espn.go.com%2Fffl%2Ffreeagency%3FteamId%3D1%26leagueId%3D39669&e=1">
<input type="hidden" name="aff_code" value="espn_fantgames">
<input type="hidden" name="appRedirect" value="http://games.espn.go.com/ffl/freeagency?teamId=1&leagueId=39669">
<input type="hidden" name="cookieDomain" value=".go.com">
<input type="hidden" name="multipleDomains" value="true">
<NOSCRIPT><input type=hidden name=noscript value=true></NOSCRIPT>
<table width=100% border=0 cellpadding=0 cellspacing=0 class="bodyCopy"><tr>
<td width=15%><b>MEMBER NAME:</b></td>
<td width=1%>&nbsp;&nbsp;</td>
<td><input name="username" size="16" maxlength="64" value="" class="select"></td>
</tr>
<tr>
<td><b>PASSWORD:</b></td>
<td>&nbsp;&nbsp;</td>
<td><input type="password" name="password" size="16" maxlength="25" value="" class="select"></td>
</tr>
<tr>
<td colspan=2>&nbsp;</td>
<td><input type="submit" name="submit" value="Log In" class="select"></td>
</tr>
</form>

Here's my python program: 这是我的python程序:

import time
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.support.ui import WebDriverWait # available since 2.4.0
from selenium.webdriver.support import expected_conditions as EC # available since 2.26.0

driver = webdriver.Chrome()
driver.get("http://games.espn.go.com/ffl/signin?redir=http%3A%2F%2Fgames.espn.go.com%2Fffl%2Ffreeagency%3FteamId%3D1%26leagueId%3D39669")

username = driver.find_element_by_name("username")
username.send_keys("*******")

password = driver.find_element_by_name("password")
password.send_keys("*******")

When I try to find the "username" element, I get a NoSuchElementException. 当我尝试查找“用户名”元素时,出现了NoSuchElementException。 I've tried looking it up online. 我尝试过在线查找。 Others have suggested switching frames, however, the login form doesn't seem to be in another frame. 其他人建议切换框架,但是,登录表单似乎不在另一个框架中。

Any ideas? 有任何想法吗?

Below code is working fine to me-- implement wait, it is mandatory in this case let the page be loaded before any DOM query! 下面的代码对我来说很好用- 实施等待,在这种情况下必须让页面在任何DOM查询之前加载!

import time
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.support.ui import WebDriverWait # available since 2.4.0
from selenium.webdriver.support import expected_conditions as EC # available since 2.26.0
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()

driver.get("http://games.espn.go.com/ffl/signin")
#implement wait it is mandatory in this case
WebDriverWait(driver,1000).until(EC.presence_of_all_elements_located((By.XPATH,"(//iframe)")))
frms = driver.find_elements_by_xpath("(//iframe)")

driver.switch_to_frame(frms[2])
time.sleep(2)
driver.find_element_by_xpath("(//input)[1]").send_keys("username")
driver.find_element_by_xpath("(//input)[2]").send_keys("pass")
driver.find_element_by_xpath("//button").click()
driver.switch_to_default_content()
time.sleep(4)
driver.close()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM