I am trying to use selenium + python to enter credit card values into a Shopify site. The boxes to enter the card values are in an iframe and I am unsure how to switch to this iframe.
I currently have this code:
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,
'//*[@id="card-fields-number-950kvfi9pbn00000"]'))
).send_keys(card_number, Keys.TAB, name_on_card, Keys.TAB,expiry_date, cvv)
driver.switch_to.default_content()
But this returns the error:
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it(...
selenium.common.exceptions.TimeoutException: Message:
So effectively, the element could not be found…
This is the HTML of the page:
(https://gyazo.com/80d9d3c941c62ededc81d5fbc327a71f)
I would like some help on how to access this element, I have also tried accessing it by changing the id to a parent of this tag. I have also added a time.sleep(20)
so I can be sure the page has fully loaded and I still got the same error.
3
Answers
The problem was that the name and id of the element are dynamic and change for each unique checkout window this is a working code:
You can switch to an iframe using the
switch_to_frame
method.You should try
driver.switch_to_frame("Insert 'Name'of Iframe here")
, then find your element. After finding the element. You can try:But I’d advise search for each element instead of using the tab key. It’ll make it easier to fix or adjust in the future as websites can change at any time. Essentially, You can use if statements to detect the fields,and if their present, fill them in.