gpt4 book ai didi

python - 使用 Selenium 登录 ESPN

转载 作者:太空宇宙 更新时间:2023-11-03 17:19:26 25 4
gpt4 key购买 nike

我正在尝试使用 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。我试过在网上查一下。其他人建议切换框架,但是,登录表单似乎不在另一个框架中。

有什么想法吗?

最佳答案

下面的代码对我来说工作正常--实现等待,在这种情况下是强制性的让页面在任何 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()

关于python - 使用 Selenium 登录 ESPN,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33326459/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com