python - 没有HTML的弹出网页上的Selenium登录
问题描述
在我的 Selenium 程序中,我使用 google-chrome 驱动程序在此网页上访问并登录我的帐户。问题是登录网页是一个带有弹出窗口的程序,没有我可以抓取的任何 HTML 代码。因此我不知道如何输入我的凭据。
我环顾四周,看到了几个带有“行动”的提案,例如
actions = ActionChains(driver)
actions.send_keys('skdkds')
actions.perform()
但问题是它没有输入任何内容,它只是将光标放在第一个字段中。我想这是程序安全性的一部分,但是通过能够将光标放在正确的位置,我应该能够将密钥发送给驱动程序,然后与 tab 命令一起移动。但我不知道怎么做。
对于一个可重现的例子:
from bs4 import BeautifulSoup
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
from webdriver_manager.chrome import ChromeDriverManager
import pandas as pd
import datetime
import time
import glob
import os
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get(link_mentionned_above) //then I am blocked
更新
正如 Azy_Crw4282 所提到的,问题在于处理警报,但是驱动程序无法识别任何警报,因为driver.switch_to_alert()
我收到了错误:
File "/usr/local/lib/python3.6/dist-packages/selenium/webdriver/common/alert.py", line 67, in text
return self.driver.execute(Command.W3C_GET_ALERT_TEXT)["value"]
File "/usr/local/lib/python3.6/dist-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/usr/local/lib/python3.6/dist-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoAlertPresentException: Message: no such alert
解决方案
您下面的代码不正确,因为它没有明确处理提示警报。
actions = ActionChains(driver)
actions.send_keys('skdkds')
actions.perform()
更改如下
actions.switchTo().alert().sendKeys("Username");
actions.switchTo().alert().sendKeys("Password");
推荐阅读
- php - 如何在 wordpress 中添加自定义导航侧边栏
- r - 在 Shiny flexdashboard 中使用 textInput
- python - 弹性搜索行为
- java - 使用 MyBatis XML Mapper 检索数据以在单个查询中填充列表
- java - 无法使用带有 maven 的 JSF 项目连接到 MS SQL
- javascript - 如何创建一个通用的 TypeScript 接口,该接口匹配任何类型/接口/对象,但对其中的值的类型有限制?
- matplotlib - 如何绘制按不同颜色分组的多个时间序列?
- windows - 使用 Vboxmanage guestcontrol 执行应用程序不会显示在来宾上
- amazon-web-services - 在 Terraform 中使用 AWS 的完全私有证书?
- swift - SwiftUI - 调整 ScrollView 滚动指示器插图