python - 在Selenium中,如何使用python在弹出窗口上登录网站?

标签 python selenium

我想使用selenium登录网站。 该网站有链接(成员(member)登录),当我单击此链接时,会出现弹出窗口。

网站网址为http://affiliates.888.com/

我编写的代码如下。

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

eight88 = webdriver.Chrome()
eight88.get("http://affiliates.888.com/")
assert "Earn Real Money" in eight88.title
loginForm = eight88.find_element_by_class_name("hide-under-480").click()
# so far popup appears.
eight88.switch_to_alert()
eight88.find_element_by_id("userName").send_keys("username")
eight88.find_element_by_id("password").send_keys("password")

当我运行此脚本时,发生NoSuchElementException

raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"id","selector":"userName"}
  (Session info: chrome=59.0.3071.115)
  (Driver info: chromedriver=2.30.477691 (6ee44a7247c639c0703f291d320bdf05c1531b57),platform=Linux 4.4.0-83-generic x86_64)

我不知道如何转到弹出窗口并在那里找到元素。

如何在弹出窗口上登录该网站。

最佳答案

授权表单位于iframe内,而不是警报。为了能够处理 iframe 内的元素,您应该首先切换到它:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait as wait

eight88 = webdriver.Chrome()
eight88.get("http://affiliates.888.com/")
assert "Earn Real Money" in eight88.title
loginForm = eight88.find_element_by_class_name("hide-under-480")
loginForm.click()

wait(eight88, 10).until(EC.frame_to_be_available_and_switch_to_it(eight.find_element_by_xpath('//iframe[contains(@src, "Auth/Login")]')))

eight88.find_element_by_id("userName").send_keys("username")
eight88.find_element_by_id("password").send_keys("password")

关于python - 在Selenium中,如何使用python在弹出窗口上登录网站?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44993540/

相关文章:

python - 使用没有对象映射的 findObject() 或 waitForObject() 通过 id 或 objectName 在 Squish 中获取 QML 元素

python - 我想使用 scrapy python 单击网站链接

python - 使用 for 循环在数据框中添加值

python - 在python中使用beautiful soup和selenium解析html

python - Elasticsearch请求

python - pandas Dataframe 上的条件搜索

C# Selenium,如何检查页面 <div> 上是否存在文本

python - Selenium 网格 - Python 文件路径

Python - 大整数的精度损失

python - Heroku & Django : "OSError: No such file or directory: '/app/{myappname}/static'"