Joe
Joe

Reputation: 59

handling frames with selenium in python

I am new to using selenium and I am currently struggling in trying to collect some data which is behind a login and inside a frame (html at bottom). The data I want to collect is inside the '#document'part of the code, can someone explain how to go about getting that?

It is not clear to me if this is inside the "MembersHostFrame" or not?

Would I need to use this code -

driver.switch_to.default_content()
driver.switch_to.frame("MembersHostFrame")

code

Upvotes: 1

Views: 1262

Answers (3)

SeleniumUser
SeleniumUser

Reputation: 4177

You can use below code to switch on to the frame.

iframe=driver.find_element_by_id("MemberHostFrame")
driver.switch_to.frame(iframe)

You can use below code to switch back to main window :

driver.switch_to.default_content()

Updated Section to wait for presence frame :

wait = WebDriverWait(driver, 20)
wait.until(EC.presence_of_element_located((By.ID, "MemberHostFrame")))

Note:: :: Please add below imports to your solution

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

Working code:

from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.ui import WebDriverWait
from selenium import webdriver
driver = webdriver.Chrome(executable_path=r"path for chromedriver.exe")
driver.maximize_window()
wait = WebDriverWait(driver, 10)
driver.get("https://members.bet365.com/members/services/host?Microsite=Members&MrsReq=True&DisplayMode=Desktop&prdid=1&platform=1&lng=1&mh=2&ptqs=%2Fhe%2FAuthenticated%2FHistory%2FDisplay%2F%3Frt%3D2%26ht%3D4")

WebDriverWait(driver, 30).until(
                EC.presence_of_all_elements_located((By.ID, "MembersHostFrame")))
iframe=driver.find_element_by_tag_name("iframe")
driver.switch_to.frame(iframe)
wait.until(EC.element_to_be_clickable((By.NAME, "ctl00$Main$login$UserName"))).send_keys("Example123")

Output:

enter image description here

Upvotes: 2

KunduK
KunduK

Reputation: 33384

To access element inside an iframe you need to switch to iframe first.

Induce WebDriverWait() and wait for frame_to_be_available_and_switch_to_it() and use either following ID,Name, Xpath or css selector.

ID:

WebDriverWait(driver,10).until(EC.frame_to_be_available_and_switch_to_it((By.ID,"MembersHostFrame")))

Name:

WebDriverWait(driver,10).until(EC.frame_to_be_available_and_switch_to_it((By.NAME,"MembersHostFrame")))

Xpath:

WebDriverWait(driver,10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[@id='MembersHostFrame']")))

Css Selector:

WebDriverWait(driver,10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"#MembersHostFrame")))

You need to import following libraries.

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

Upvotes: 1

Sri
Sri

Reputation: 2318

I think this should answer your question. Select iframe using Python + Selenium

You would need to switch into the iframe, and then locate the element. Your assumption is correct. Based on the comment in the link provided, you may need to use xpath to get the data from within the iframe.

Upvotes: 0

Related Questions