使用 python 和 selenium 在 Palo Alto 网站上发送 2FA 电子邮件

问题描述 投票:0回答:1

我正在尝试编写一个脚本,该脚本将从帕洛阿尔托网络网站下载文件。 我正在编写的脚本成功输入了用户名和密码,但是随后出现了 2FA 部分,并且我无法让 selenium 找到/按下“向我发送代码”按钮。

我到目前为止的脚本如下:

import os
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from dotenv import dotenv_values

# Load the .env file into a variable
config = dotenv_values(".env")

# Read username and password from the environment file
username = config['USERNAME']
password = config['PASSWORD']

# URL of the Palo Alto login page
url = "https://sso.paloaltonetworks.com/"

# Path to your WebDriver (download the appropriate one for your browser)
# For Chrome, download from https://sites.google.com/chromium.org/driver/
# webdriver_path = r"c:\Users\tdadmin\Documents\Python\PaloAlto\RegListDownload\chromedriver-win64\chromedriver.exe"

# Create a new instance of the Chrome driver
driver = webdriver.Chrome()

try:
    # Open the login page
    driver.get(url)

    # Find the username input field and fill them
    username_field = driver.find_element(By.ID, "idp-discovery-username")
    username_field.send_keys(username)

    # Submit the username
    username_field.send_keys(Keys.RETURN)

    # Wait for the password field to load
    WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.ID, "okta-signin-password"))
    )

    # Find the password input field and fill them
    password_field = driver.find_element(By.ID, "okta-signin-password")
    password_field.send_keys(password)

    # Submit the password
    password_field.send_keys(Keys.RETURN)

    # Wait for the 2FA page to load
    WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.CSS_SELECTOR, "button button-primary"))
    )

    # Press the button to send the e-mail
    send_2FA = driver.find_element(By.CSS_SELECTOR, "button button-primary")
    send_2FA.click();

    # Provide an opportunity for the user to enter 2FA code manually
    # input("Please enter your 2FA code, then press Enter to continue...")

    # You may need to locate and interact with the "Submit" button after entering 2FA code

    # For example:
    # submit_button = driver.find_element(By.ID, "submit_button_id")
    # submit_button.click()

    # Now, you can perform actions on the authenticated session
    # (e.g., navigate to the desired page and download a file)

finally:
    # Close the browser window
    driver.quit()

我想按的“按钮”的xpath是: /html/body/div/div[2]/div/div[2]/div[1]/main/div[2]/div/div/form/div[2]/input

python selenium-webdriver button xpath
1个回答
0
投票

我认为按钮的 css 选择器应该是

".button.button-primary"

您当前的CSS选择器(“按钮button-primary”)尝试在该元素中找到一个

element
<button/>
en,找到一个元素
<button-primary/>

在 css 中,您可以通过在类名前面使用

.
来选择类。如果您想查找具有多个类的元素,则应将它们粘在一起而不使用空格。

© www.soinside.com 2019 - 2024. All rights reserved.