1

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

3 Answers 3

2

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

Sign up to request clarification or add additional context in comments.

4 Comments

getting this error message NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="MemberHostFrame"]"} (Session info: chrome=80.0.3987.163)
Coule be site is taking time to load iframe, Can you please provide yourr site ? I will and update you accordingly
This is the site - members.bet365.com/members/services/… - but it is behind a login...
@Joe : Please find working solution section and once you verify would you kind enough to accept answer and hit upvote button from your end.
1

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

Comments

0

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.

Comments

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.