Pietro Tamburini
Pietro Tamburini

Reputation: 91

switch iframe selenium Python without ID

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.

Upvotes: 1

Views: 1273

Answers (1)

0buz
0buz

Reputation: 3503

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()

Upvotes: 2

Related Questions