1

I'm trying to find an input element in Selenium with Python. I can't find it because the input is inside of an iframe. I have try to switch in that iframe but I don't have an ID of iframe. below the code:

<iframe sandbox="allow-scripts allow-same-origin" class="credit-card-iframe-cvv mt1 u-full-width" src="https://.........." frameborder="0" scrolling="no"></iframe>

and this is the code of the input:

<input type="tel" id="cvNumber" tabindex="0" data-shortname="cvv" maxlength="4" placeholder="XXX" class="mod-input ncss-input pt2-sm pr4-sm pb2-sm pl4-sm" autocomplete="off" value="">

I don't know how to switch because there isn't the ID of the iframe.

I need to write inside of the input the CVV code with send keys.

1 Answer 1

2

You could use xpath to find the iframe, so something like "//iframe[@class='credit-card-iframe-cvv mt1 u-full-width']" as your find_by_xpath condition.

my_iframe=driver.find_element_by_xpath("//iframe[@class='credit-card-iframe-cvv mt1 u-full-width']")
driver.switch_to.frame(my_iframe)

A more robust version of the above would be:

from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait

my_iframe=WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//iframe[@class='credit-card-iframe-cvv mt1 u-full-width']")))
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it(my_iframe))

Switch back to default content like so:

driver.switch_to.default_content()
Sign up to request clarification or add additional context in comments.

3 Comments

You can't send keys to iframe. First you identify and switch to the desired iframe. Then you identify and send keys to the desired input.
Thanks so much it's work..... How can i turn back out of the iframe?
Added driver.switch_to.default_content() to answer body.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.