具有 Windows 安全性的 Python Selenium 网页

2024-01-05

我正在尝试定期从我组织的网站自动下载一些 CSV。他们非常友善,没有为我提供后端数据库访问或 API,所以我不得不拼凑一些东西来帮我处理这个问题。该网站是一个 Oracle PeopleSoft 网站,会提示Windows Security在页面加载之前就出现模态。

我使用 Edge 是因为该网站似乎不喜欢 Firefox,而 Chrome 在 Selenium 中给我带来了麻烦。我过去曾能够以这种方式从 OBIEE 网站上抓取信息,但这一次给我带来了麻烦。下面的代码是我用来访问页面并尝试处理登录模式的代码。单步执行我的代码似乎我没有超越driver.get(url)根本就行。

有人对如何处理这个问题有建议吗?

driver = webdriver.Edge(EdgeDriverManager().install())
driver.get(url)
# Wait till the modal prompts you to log in
wait(driver, 5).until(EC.alert_is_present())
alert = driver.switch_to_alert()
# Provide creds with a tab in between so that you change from username field to password field
alert.send_keys(config.myUSERNAME + Keys.TAB + config.myPASSWORD)
# click ok
alert.accept()

编辑:2018 年 1 月 25 日

尝试按照建议使用 Autoit,但仍然遇到问题。网络驱动程序似乎不允许在运行时发生任何其他事情。关于如何处理这个问题有什么建议吗?

def browser(url):
    driver = webdriver.Edge(EdgeDriverManager().install())
    driver.get(url)


def login_handler(username, password):
    print('This print never gets run? What is up with this?!')
#     autoit.win_wait_active("Credential Dialog Xaml Host")
    autoit.win_exists("Windows Security", "CORP\\")
#     also tried this
#     autoit.win_wait_active("Windows Security")
    autoit.send(username)
    autoit.send("{TAB}")
    autoit.send(password)
    autoit.send("{ENTER}")

t1 = Thread(target=browser(url))
t2 = Thread(target=login_handler(config.myUSERNAME, config.myPASSWORD))
t2.start()
t1.start()

看来多线程 Selenium 和 Autoit 是不可能的,或者至少我还没有弄清楚(请随意证明我错了......请)。我可以通过从 selenium 脚本运行 autoit 脚本来回避这个问题。

windows_securit_login_handler.py

import autoit
import os
import config
import datetime
import time

def login_handler(username, password):
    """
     Hey Windows Security People this is what I think of you pesky Windows Security modal (ಠ_ಠ)┌∩┐
     Python to rule the world!
    :param username: String
    :param password: String
    :return:
    """
    # just chill out and wait to see a window with the title 'Windows Security
    autoit.win_wait("Windows Security")
    # now make that crap active so we can do stuff to it
    autoit.win_activate("Windows Security")
    # type in the username
    autoit.send(username)
    # tab to the password field
    autoit.send("{TAB}")
    # type in the password
    autoit.send(password)
    # kill!
    autoit.send("{ENTER}")

if __name__ == "__main__":
    # cause this is not thread safe or I am too ignorant to figure out how to do this in single python program
    # I am running this in the background persistently so that the scraper doens't ever have to worry about the 
    # silly Windows Security modal

    while True:

        try:
            login_handler(config.myUSERNAME, config.myPASSWORD)
        except:
             print("Got some ERROR @ {} \nAnd I'm too laszy to figure out how to deal with it properly so you get this message.".format(datetime.datetime.now()))

刮刀.py

from webdriver_manager.driver import EdgeDriver
from webdriver_manager.microsoft import EdgeDriverManager
from selenium import webdriver
import sys
import os

os.system('python windows_securit_login_handler.py')
driver = webdriver.Edge(EdgeDriverManager().install())
driver.get(url)
... more code to do scraping type things ... 
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

具有 Windows 安全性的 Python Selenium 网页 的相关文章

随机推荐