Reputation: 49
I am trying to click an iframe popup window, but can not get it in focus.
Here is the popup window and the button I am trying to click:
Here is the code I have so far:
patsearch = driver.find_element_by_xpath('//*[@id="command_idSearchButton"]/nobr').click()
time.sleep(5)
iframe = driver.find_element_by_xpath('//*[@id="iframe_52"]')
driver.switch_to.frame(iframe)
mrn = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="control_15"]'))).send_keys(mrnum)
time.sleep(1)
ln = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="control_18"]'))).send_keys(last_name)
time.sleep(1)
fn = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="control_19"]'))).send_keys(first_name)
time.sleep(1)
search = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="control_66"]'))).click()
#try: click patient if one exists / try warning pop alert / exception warning / exception nopatient exists wrtie to file
try:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="idHTMLResult"]/table/tbody/tr[2]/td[4]/a'))).click()
try:
print('trying to switch..')
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.ID, "frame_116")))
print('switched')
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="control_8"]')))
driver.switch_to_default_content()
except TimeoutException:
print('didnt switch...')
Here is the HTML:
Solution:
iframe = driver.find_element_by_xpath('//*[@id="iframe_52"]')
driver.switch_to.frame(iframe)
#do something
#try: click account if one exists / try warning pop alert / exception warning / exception no account exists write to file
try:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="idHTMLResult"]/table/tbody/tr[2]/td[4]/a'))).click()
try:
driver.switch_to_default_content()
print('trying to switch..')
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.ID, "frame_116")))
print('switched')
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="control_8"]'))).click()
driver.switch_to_default_content()
except:
driver.switch_to.frame(iframe)
Upvotes: 1
Views: 540
Reputation: 33384
Could you please try this and check whether it works.instead of ID can you try with class name and check whether it works.
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CLASS_NAME, "modalGroupIframe")))
driver.find_element_by_xpath('//div[@class="modalGroupIframe"]')
Upvotes: 0
Reputation: 6459
Use the explicit wait for locating iframe
and switching to it:
from selenium.webdriver.support import ui
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
ui.WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.ID, "frame_116")))
PS: This code will replace the following lines:
warning = driver.find_element_by_xpath('//*[@id="frame_116"]')
driver.switch_to_frame(warning)
Hope it helps you!
Upvotes: 2