Warm tip: This article is reproduced from serverfault.com, please click

python-如何使用 selenium 在下拉列表中选择项目?

(python - How to select item in dropdown list using Selenium?)

发布于 2020-11-28 22:05:40

亲爱的叠朵花,

我正在制作一个脚本,该脚本会自动收回我的CC。我在抄送的网页上的下拉列表中选择一个选项时遇到麻烦。除了下拉选项,该代码告诉脚本选择我的支票作为付款方式,下面的代码具有完成付款的所有步骤。

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

import yaml
import time
conf = yaml.load(open(r'D:\Users\Matt\Documents\GitHub\YML_Files\REI_Login_Credentials.yml'))
myREIUsername = conf['REILogin']['username']
myREIPassword = conf['REILogin']['password']

driver = webdriver.Firefox(
    executable_path=
    r'D:\Users\Matt\Documents\GitHub\Executable_Files\geckodriver.exe'
)

def login():
   driver.get('https://onlinebanking.usbank.com/Auth/Login?usertype=REIMC&redirect=login&lang=en&exp=')
   time.sleep(4)
   driver.find_element_by_id('aw-personal-id').send_keys(myREIUsername)
   driver.find_element_by_id('aw-password').send_keys(myREIPassword)
   time.sleep(2)
   driver.find_element_by_id('aw-log-in').click()
   time.sleep(10)
   make_payment()

def make_payment():
    if (driver.find_element_by_class_name('accountRowLast').text) != "0.00":
        driver.find_element_by_css_selector('a.soloLink.accountNamesize').click()
        time.sleep(3)
        driver.find_element_by_xpath('/html/body/div[4]/div[2]/div[2]/div[2]/div[4]/div[3]/div[10]/div[2]/div[2]/a').click()
        time.sleep(10)
        driver.find_element_by_id('fromAccountDropDown-button').click()

        driver.find_element_by_id('rdoAmountOptionSingleCurrent_1')
        driver.find_element_by_id('btnTPContinue').click()
    else:
        driver.quit()

login()

这是我要选择的屏幕截图,以及该部分的HTML。

在此处输入图片说明

这是显示id标签中选项的屏幕截图。 ap

我仍在学习如何使用webdriverwait,因此目前只有time.sleep()作为等待方法。

任何帮助,将不胜感激!

Questioner
Hiebs915
Viewed
0
Hiebs915 2020-12-03 02:57:54

我能够使它工作,尽管有点of回。我使用了Pynput和一些键盘输入来滚动下拉菜单,然后选择我要从中付款的帐户。它不是完美的,但它是一个开始,并且可以正常工作。这是我使用的最终代码:

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 selenium.common.exceptions import NoSuchElementException
from pynput.keyboard import Key, Controller

import yaml
import time

conf = yaml.load(open(r'D:\Users\Matt\Documents\GitHub\YML_Files\REI_Login_Credentials.yml'))
myREIUsername = conf['REILogin']['username']
myREIPassword = conf['REILogin']['password']

driver = webdriver.Firefox(
    executable_path=
    r'D:\Users\Matt\Documents\GitHub\Executable_Files\geckodriver.exe'
)

def login():
   driver.get('https://onlinebanking.usbank.com/Auth/Login?usertype=REIMC&redirect=login&lang=en&exp=')
   time.sleep(4)
   driver.find_element_by_id('aw-personal-id').send_keys(myREIUsername)
   driver.find_element_by_id('aw-password').send_keys(myREIPassword)
   time.sleep(2)
   driver.find_element_by_id('aw-log-in').click()
   time.sleep(10)
   make_payment()

def make_payment():
    if (driver.find_element_by_class_name('accountRowLast').text) != "0.00":
        driver.find_element_by_css_selector('a.soloLink.accountNamesize').click()
        time.sleep(3)
        driver.find_element_by_xpath('/html/body/div[4]/div[2]/div[2]/div[2]/div[4]/div[3]/div[10]/div[2]/div[2]/a').click()
        time.sleep(5)
        accounts = driver.find_elements_by_class_name('ui-selectmenu-status')
        accounts[1].click()
        keyboard = Controller()
        keyboard.press(Key.down)
        time.sleep(1)
        keyboard.release(Key.down)
        keyboard.press(Key.enter)
        keyboard.release(Key.enter)
        driver.find_element_by_id('rdoAmountOptionSingleCurrent_1').click()
        driver.find_element_by_id('btnTPContinue').click()
        time.sleep(1)
        driver.find_element_by_id('btnSubmit').click()
        driver.quit()
    else:
        driver.quit()

login()