简体   繁体   English

在 python 中使用 selenium 处理帧

[英]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).我是使用 selenium 的新手,我目前正在努力收集登录背后和框架内的一些数据(底部的 html)。 The data I want to collect is inside the '#document'part of the code, can someone explain how to go about getting that?我要收集的数据在代码的“#document”部分中,有人可以解释一下 go 的获取方法吗?

It is not clear to me if this is inside the "MembersHostFrame" or not?我不清楚这是否在"MembersHostFrame"内?

Would I need to use this code -我是否需要使用此代码 -

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

代码

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:您可以使用以下代码切换回主 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: Output:

在此处输入图像描述

I think this should answer your question.我认为这应该回答你的问题。 Select iframe using Python + Selenium Select iframe 使用 Python + Selenium

You would need to switch into the iframe, and then locate the element.您需要切换到 iframe,然后找到该元素。 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.根据提供的链接中的注释,您可能需要使用 xpath 从 iframe 中获取数据。

To access element inside an iframe you need to switch to iframe first.要访问iframe中的元素,您需要先切换到iframe

Induce WebDriverWait () and wait for frame_to_be_available_and_switch_to_it () and use either following ID , Name , Xpath or css selector.诱导WebDriverWait () 并等待frame_to_be_available_and_switch_to_it () 并使用以下IDNameXpathcss选择器。

ID: 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 : Xpath

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

Css Selector: Css 选择器:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM