qwertylpc
qwertylpc

Reputation: 2106

Python Selenium Xpath from firebug not found

I am trying to login to the ESPN website using selenium. Here is my code thus far

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Firefox()
driver.maximize_window()

url = "http://www.espn.com/fantasy/"
driver.get(url)

login_button = driver.find_element_by_xpath("/html/body/div[6]/section/section/div/section[1]/div/div[1]/div[2]/a[2]")
login_button.click()

try:
    element = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.XPATH, "/html/body/div[2]/div/div/section/section/form/section/div[1]/div/label/span[2]/input")))
except:
    driver.quit()

Basically, there are 2 steps, first I have to click the login button and then I have to fill in the form. Currently, I am clicking the login button and the form is popping up but then I can't find the form. I have been using firebug to get the xpath as suggested in other SO questions. I don't really know much about selenium so I am not sure where to look

Upvotes: 1

Views: 159

Answers (2)

Andersson
Andersson

Reputation: 52665

Try to use

driver.switch_to_frame('disneyid-iframe')
# handle authorization pop-up
driver.switch_to_default_content() # if required

Upvotes: 2

Levi Noecker
Levi Noecker

Reputation: 3300

This works for me, switching to the iframe first. Note that you will need to switch back out of the iframe after entering the credentials.

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Firefox()
driver.maximize_window()

url = "http://www.espn.com/fantasy/"
driver.get(url)

login_button = driver.find_element_by_xpath("/html/body/div[6]/section/section/div/section[1]/div/div[1]/div[2]/a[2]")
login_button.click()

iframe = driver.find_element_by_id("disneyid-iframe")
driver.switch_to.frame(iframe)

try:
    element = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.XPATH, "/html/body/div[2]/div/div/section/section/form/section/div[1]/div/label/span[2]/input")))
    element.send_keys("my username")

    import time
    time.sleep(100)
finally:
    driver.quit()

Upvotes: 2

Related Questions