带有Windows安全性的Python Selenium网页

时间:2018-01-12 15:39:07

标签: python-3.x selenium web-scraping

我正在尝试从我的组织的网站定期自动下载一些CSV。他们非常友善,不能负担我的后端数据库访问权限或API,所以我不得不蹒跚学步,为我解决这个问题。该网站是一个Oracle PeopleSoft网站,在页面加载之前会提示Windows Security模式。

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

有没有人有关于如何处理这个问题的建议?

enter image description here

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()

编辑:01/25/2018

尝试按照建议使用自动,我仍然遇到问题。 webdriver在运行时似乎不允许任何其他事情发生。有关如何处理这个的任何建议?

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()

1 个答案:

答案 0 :(得分:0)

看来它不可能是多线程的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()))

scraper.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 ...