我试图在弹出窗口中的用户名和密码框中输入一些文本 https://fantasy.espn.com/football/players/add?leagueId=1
我假设有一个弹出窗口,所以我相信我切换到了它。我找不到任何可以选择的元素。我试过做
driver.find_element_by_xpath("//input[@type='email']")
或
driver.find_element_by_xpath("//input[@placeholder='Username or Email Address']")
两次我都收到错误,指出找不到元素:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
driver = webdriver.Chrome()
driver.get("https://fantasy.espn.com/football/players/add?leagueId=1")
time.sleep(15)
driver.find_element_by_xpath("//input[@placeholder='Username or Email Address']")
弹出窗口正在iframe
内加载。您必须首先切换到 iframe,然后使用 xpath 或其他选择器查找其余字段。
from selenium.webdriver import Chrome
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = Chrome('drivers/chromedriver')
driver.get('https://fantasy.espn.com/football/players/add?leagueId=1')
iframe_xpath = '//*[@id="disneyid-iframe"]'
iframe = WebDriverWait(driver, 30).until(
EC.element_to_be_clickable(
(By.XPATH, iframe_xpath)
)
)
driver.switch_to_frame(iframe)
username_field = driver.find_element_by_xpath("//input[@type='email']")
password_field = driver.find_element_by_xpath("//input[@type='password']")
login_button = driver.find_element_by_xpath(
'//*[@id="did-ui-view"]/div/section/section/form/section/div[3]/button'
)
username_field.send_keys('username')
password_field .send_keys('password')
login_button.click()
编辑
搜索玩家
from selenium.webdriver import Chrome
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
capabilities = DesiredCapabilities.CHROME
capabilities["pageLoadStrategy"] = "none"
driver = Chrome('drivers/chromedriver', desired_capabilities=capabilities)
driver.get('https://fantasy.espn.com/football/leaders')
player_xpath = '//*[@id="espn-analytics"]/div/div[5]/div[2]/div[1]/div[1]/div/div[2]/div[1]/div/div/div/input'
player_input = WebDriverWait(driver, 20).until(
EC.element_to_be_clickable(
(By.XPATH, player_xpath)
)
)
player_input.send_keys('chrisn')