I have a problem to find element on credit card payment form, which contains a number that changes each time, so please help me to find the way to do it.
This is the element:
<input id="adyen-checkout-encryptedCardNumber-1644589302666" data-fieldtype="encryptedCardNumber" type="text" inputmode="numeric" maxlength="24" autocomplete="cc-number" placeholder="1234 5678 9012 3456" aria-label="Champ du numéro de carte" aria-invalid="true" aria-required="true" aria-describedby="adyen-checkout-encryptedCardNumber-1644589302666-ariaError" data-type="gsf" style="display: block;">
CodePudding user response:
Various locator strategies you can use (as per the DOM provided in the query):
All are xpaths
//input[@data-fieldtype='encryptedCardNumber']
//input[@type='text']
//input[@inputmode='numeric']
//input[@aria-label='Champ du numéro de carte']
CodePudding user response:
This HTML...
<input id="adyen-checkout-encryptedCardNumber-1644589302666" data-fieldtype="encryptedCardNumber" type="text" inputmode="numeric" maxlength="24" autocomplete="cc-number" placeholder="1234 5678 9012 3456" aria-label="Champ du numéro de carte" aria-invalid="true" aria-required="true" aria-describedby="adyen-checkout-encryptedCardNumber-1644589302666-ariaError" data-type="gsf" style="display: block;">
...indicates a Creditcard Number field.
Generally Creditcard Number fields are within an <iframe>
. Hence to access the Creditcard Number field you have to:
Induce WebDriverWait for the desired frame to be available and switch to it.
Induce WebDriverWait for the desired element to be clickable.
You can use either of the following locator strategies:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe_css"))) WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[data-fieldtype='encryptedCardNumber'][aria-label='Champ du numéro de carte']"))).send_keys("1234567890")
Using XPATH:
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"iframe_xpath"))) WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@data-fieldtype='encryptedCardNumber' and @aria-label='Champ du numéro de carte']"))).send_keys("1234567890")
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC
Reference
You can find a couple of relevant discussions in:
- How to enter date in the credit card number field using Selenium and Python?
- Switch to an iframe through Selenium and python
CodePudding user response:
Thank you @undetected Selenium for you response it was a frame problem so this is what i do and its works fine for me :
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, "//*[@id='adyen-card-container']/div/div/div[2]/div[1]/div[1]/label/div/span/iframe")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH,"//input[@data-fieldtype='encryptedCardNumber']"))).send_keys("0000111122223333")
##switch parent frame
self.driver.switch_to.default_content()
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it(
(By.XPATH, "//*[@id='adyen-card-container']/div/div/div[2]/div[1]/div[2]/div[1]/label/div/span/iframe")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH,"//input[@data-fieldtype='encryptedExpiryDate']"))).send_keys("0228")
##switch parent frame
self.driver.switch_to.default_content()
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it(
(By.XPATH, "//*[@id='adyen-card-container']/div/div/div[2]/div[1]/div[2]/div[2]/label/div/span/iframe")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@data-fieldtype='encryptedSecurityCode']"))).send_keys("123")