DaniP
DaniP

Reputation: 25

How to find XPath for button within iframe using python?

I have the following html object inside an iframe:

html code for 'SUBMIT' button

I need to find it's XPath in order to click on the "SUBMIT" button but cannot find it. XPath helper only shows "//iframe".

So far, I've tried:

submit = driver.find_elements_by_xpath("//iframe[@id='btnSubmit']")
submit.click

Upvotes: 2

Views: 1092

Answers (1)

Andrei
Andrei

Reputation: 5637

All content, which is inside a frame or iframe cannot be accesed without switching to iframe/frame. So firstly switch to frame content:

driver.switch_to.frame(driver.find_element_by_name("frame_name"))

or

driver.switch_to.frame(driver.find_element_by_xpath("//xpath/to/frame"))

Then you can locate your submit button and click on it:

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//*[@id='btnSubmit']"))).click()

Than switch to default content like this:

driver.switch_to.default_content()

Note: you have to add some imports:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

Upvotes: 1

Related Questions