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

2024-10-08 18:30:52 发布

您现在位置:Python中文网/ 问答频道 /正文

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

网站URL是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发生。在

^{pr2}$

我不知道如何在弹出窗口中找到元素。在

我怎样才能登录这个网站弹出。在


Tags: fromimportcomsendidhttpby网站
2条回答

授权表单位于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")

弹出窗口是由JavaScript生成的。所以这是一个动态页面。当你使用驱动程序。获取()您正在读取生成的第一个静态页面源代码,显然用户名和密码字段不包括在内。首先弄清楚JavaScript是如何调用弹出窗口的,然后使用selenium的exec函数执行该JavaScript,然后使用另一个函数对第一个结果进行身份验证。在

相关问题 更多 >

    热门问题