Home > OS >  issue clicking dropdown values from input
issue clicking dropdown values from input

Time:12-26

from selenium import webdriver
from selenium.webdriver.common.by import By
import time
from selenium.webdriver.support.ui import WebDriverWait



wait = WebDriverWait

driver = webdriver.Chrome()
driver.get('website')

customerid_form_elements = driver.find_elements(By.CSS_SELECTOR, "input[aria-labelledby='i1']")
for form_element in customerid_form_elements:
    form_element.send_keys("test")

charge_input = input("Charge Amount: ")
form_charge = driver.find_elements(By.CSS_SELECTOR, "input[aria-labelledby='i5']")
for charge in form_charge:
    charge.send_keys(charge_input)

user_input = input("Card & CVV '6969696969696969 666': ")
input_list = user_input.split()

text_field_1 = driver.find_element(By.CSS_SELECTOR, "input[aria-labelledby='i9']")
text_field_2 = driver.find_element(By.CSS_SELECTOR, "input[aria-labelledby='i21']")
text_field_1.send_keys(input_list[0])
text_field_2.send_keys(input_list[1])

last_four_field = driver.find_element(By.CSS_SELECTOR, "input[aria-labelledby='i25']")
card_number = input_list[0]
last_four = card_number[-4:]
last_four_field.send_keys(last_four)

expiration_date = input("Expiration date (MM): ")

dropdown_element = driver.find_element(By.XPATH, "//div[@jsname='LgbsSe']")
dropdown_element.click()

options = driver.find_elements(By.XPATH, "//div[@jsaction and starts-with(@data-value, '[01]')]")

for option in options:
    if option.get_attribute("data-value") == expiration_date:
        option.click()
        break

Everything runs fine until it has to click the data value of my input from the dropdown. Cant use a selector because its a div class. pretty confused. See image for the jsaction values im trying to click based on my from input.

HTML pic

CodePudding user response:

I've modified some of your code, please try this:

for 'Customer ID' field, there is only one match for the locator, so you need to use find_elements and for loop, you can use this:

# for Customer ID field
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[aria-labelledby='i1']"))).send_keys("test")

# for 'Expiration Month' field:
expiration_date = input("Expiration date (MM): ")  # you have to enter the value in the format - '01' or '02'... 

dropdown_element = driver.find_element(By.XPATH, "(//div[@jsname='LgbsSe'])[1]")
dropdown_element.click()
time.sleep(2)   # increased the wait time

options = driver.find_elements(By.CSS_SELECTOR, ".OA0qNb.ncFHed.QXL7Te .MocG8c.HZ3kWc.mhLiyf span")

for option in options:
    time.sleep(1)     # added wait time
    if option.text == expiration_date:
        option.click()
        break


# for Expiration Year field
expiration_month = input("Expiration Year (YYYY): ")  # you have to enter the value in the format - "2021" or "2022"...
time.sleep(1)
dropdown_element_2 = driver.find_element(By.XPATH, "(//div[@jsname='LgbsSe'])[2]")
dropdown_element_2.click()
time.sleep(1)

options = driver.find_elements(By.CSS_SELECTOR, ".OA0qNb.ncFHed.QXL7Te .MocG8c.HZ3kWc.mhLiyf span")

for option in options:
    if option.text == expiration_month:
        option.click()
        break
  • Related