skip to Main Content

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


  1. Chosen as BEST ANSWER

    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:

    iframe = driver.find_element_by_class_name('card-fields-iframe')
    driver.switch_to.frame(iframe)
    driver.find_element_by_name('number').send_keys(card_number, Keys.TAB,name_on_card,Keys.TAB,expiry_date,Keys.TAB, cvv)
    driver.switch_to.default_content()
    

  2. You can switch to an iframe using the switch_to_frame method.

    iframe = driver.find_element(By.XPATH, '//*[@id="card-fields-number-950kvfi9pbn00000"]')
    driver.switch_to_frame(iframe)
    #You're now 'in' the iframe
    
    Login or Signup to reply.
  3. You should try
    driver.switch_to_frame("Insert 'Name'of Iframe here"), then find your element. After finding the element. You can try:

    ELEMENT.send_keys(card_number, Keys.TAB, name_on_card, Keys.TAB,expiry_date, cvv)
    driver.switch_to.default_content()
    

    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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search