0

I'm trying to make a script to checkout on a Shopify site and I was able to find the iframe for the card number, but was not able to find the iframe for the name on the card (2nd iframe). Is there any way to enter in a value for that iframe?

driver.switch_to.frame(driver.find_element_by_tag_name("iframe"))
driver.find_element_by_xpath('//input[@autocomplete="cc-number"]').send_keys("1234")
driver.find_element_by_xpath('//div[@data-card-field-placeholder="Name on card"]').click()
driver.switch_to.frame(driver.find_element_by_tag_name("iframe"))
driver.find_element_by_xpath('//input[@autocomplete="cc-name"]').send_keys("First Last")

I've tried

driver.find_element_by_xpath('//input[@autocomplete="cc-name"]').send_keys("First Last")

and it gives me Unable to locate element

Image for reference: https://i.stack.imgur.com/BgeF1.jpg

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Spolt
  • 25
  • 5

3 Answers3

1

The card number and name fields are in different <iframe> so you have to:

  • Induce WebDriverWait for the desired frame to be available and switch to it for the card number field.

  • Switch to the Default Content

  • Induce WebDriverWait for the desired frame to be available and switch to it for the name field.

  • You can use either of the following based Locator Strategies:

    WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[@class='card-fields-iframe' and starts-with(@id, 'card-fields-number')]")))
    # perform other operations
    driver.switch_to.default_content()
    WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[@class='card-fields-iframe' and starts-with(@id, 'card-fields-name')]")))
    
  • 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:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0

Use unique locator as you do for any other elements

The syntax for switch to frame is

   driver.switch_to.frame(WebElement)

So you don't need use just tag name , you can use xpath , Id , css etc like anyothr element

We use tag usually because there will be only one iframe in most cases . If you have multiple iframes find each iframes uniquely as you do for other elements using any other attributes like I'd,class etc of the specific iframe

driver.switch_to.frame(driver.find_element_by_xpath('//iframe[@id="card-fields-name-fpsn1ey65to00000"]'))

PDHide
  • 18,113
  • 2
  • 31
  • 46
  • Tried this but it didn't work ```driver.switch_to.frame(driver.find_element_by_xpath('iframe[id="card-fields-name-fpsn1ey65to00000"]'))``` – Spolt Dec 04 '20 at 05:09
  • You have to put @id not id – PDHide Dec 04 '20 at 05:12
  • //iframes[@id="!$!+ – PDHide Dec 04 '20 at 05:13
  • What you have used is a css locator not xpath , read basic element locators in xpatha and css – PDHide Dec 04 '20 at 05:14
  • Please accept the answer if it helped ,also the previous one . It's not a good practice to remove accepted answers as we put time and effort to debug and help you fix your code . Thanks for understanding – PDHide Dec 04 '20 at 05:18
0

I'd suggest using the parent tag as it has a more identifiable div tag that can be xpathed to.This way you can just go to the iframe you want before finding the input tag to send keys to.

driver.switch_to.frame(driver.find_element_by_xpath("div[@data-card-fields='number']/iframe"))
Arundeep Chohan
  • 9,779
  • 5
  • 15
  • 32
  • hmm, I just tried this and it's giving me "Unable to locate element". I tried switching back to default content and back and neither work – Spolt Dec 04 '20 at 06:09