一尘不染

使用Selenium登录到ESPN

selenium

我正在尝试使用Selenium来自动执行ESPN上的某些任务,由于要在访问ESPN页面时重定向到登录页面,因此我首先需要登录到我的帐户。这是登录表单:

<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>

这是我的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("*******")

当我尝试查找“用户名”元素时,出现了NoSuchElementException。我尝试过在线查找。其他人建议切换框架,但是,登录表单似乎不在另一个框架中。

有任何想法吗?


阅读 326

收藏
2020-06-26

共1个答案

一尘不染

下面的代码对我来说很好用- 实施等待,在这种情况下,必须强制在 任何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()
2020-06-26