首页 > 解决方案 > 没有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

标签: pythonseleniumpopup

解决方案


您下面的代码不正确,因为它没有明确处理提示警报。

actions = ActionChains(driver)
actions.send_keys('skdkds')
actions.perform()

更改如下

actions.switchTo().alert().sendKeys("Username");
actions.switchTo().alert().sendKeys("Password");

推荐阅读