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

How to select item in dropdown list using Selenium?

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

Dear Stack Overflowers,

I'm making a script that automatically pays off my CC's. I'm having some trouble selecting an option in a dropdown list on the CC's webpage. The code below has every step to complete the payment except the dropdown option that tells the script to select my checking as the form of payment.

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

Here's a screenshot of what I'm trying to select and the HTML for that section.

enter image description here

Here's a screenshot showing the options in the id tag. Here's a p

I'm still learning how to use webdriverwait so I only have time.sleep() as the waiting method for now.

Any help would be appreciated!

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

I was able to get it work, although its a bit of roundabout way. I used pynput and some keyboard inputs to scroll through the dropdown and then select the account I wanted to pay from. Its not perfect but its a start and it works. Here's the final code I used:

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