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

Joe
  • 59
  • 6

3 Answers3

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

SeleniumUser
  • 4,065
  • 2
  • 7
  • 30
  • 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)``` – Joe Apr 10 '20 at 18:11
  • Coule be site is taking time to load iframe, Can you please provide yourr site ? I will and update you accordingly – SeleniumUser Apr 10 '20 at 18:14
  • This is the site - 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 - but it is behind a login... – Joe Apr 10 '20 at 19:38
  • @Joe : Please find working solution section and once you verify would you kind enough to accept answer and hit upvote button from your end. – SeleniumUser Apr 12 '20 at 00:14
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.

Sri
  • 2,281
  • 2
  • 17
  • 24
0

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
KunduK
  • 32,888
  • 5
  • 17
  • 41