在弹出窗口中输入文本,无法使用python硒找到元素

时间:2019-09-29 01:07:57

标签: python selenium-chromedriver

我试图在弹出的用户名和密码框中输入一些文本 https://fantasy.espn.com/football/players/add?leagueId=1

我认为这里有一个弹出窗口,所以我相信我已经切换到它了。我找不到我可以选择的任何元素。我尝试做

<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tool="http://schemas.android.com/tools" android:id="@+id/linearLayout" android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:id="@+id/crime_title" android:layout_width="0dp" android:layout_height="wrap_content" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" tool:text="Crime Title" /> <TextView android:id="@+id/crime_date" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginTop="18dp" android:layout_marginBottom="9dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="@+id/crime_title" tool:text="Crime Date" /> </androidx.constraintlayout.widget.ConstraintLayout>

driver.find_element_by_xpath("//input[@type='email']")

这两次我都得到错误说明元素:

driver.find_element_by_xpath("//input[@placeholder='Username or Email Address']")

1 个答案:

答案 0 :(得分:0)

弹出式窗口正在iframe中加载。您必须首先switch to the 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('chris\n')