mswis731
mswis731

Reputation: 13

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

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

Upvotes: 1

Views: 1503

Answers (1)

Learner
Learner

Reputation: 5302

Below code is working fine to me-- implement wait, it is mandatory in this case let the page be loaded before any DOM query!

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

Upvotes: 1

Related Questions