我怎样才能选择一个html元素,不管它在硒中的位置是什么? [英] How can I select a html element no matter what frame it is in in selenium?
问题描述
我试图选择一个驻留在iframe中并可能位于其他iframe中的元素。
是否有可能在(python)selenium中的某个(子)iframe内选择一个元素而不选择iframe?有没有办法'循环'每个iframe以某种方式,并检查在哪里可以找到我的元素...?
以及如何做到这一点的情况下元素和HTML东西和iframe可能只是被加载...?
否,这是不可能的通过 与
而不切换到相应的 iframe
中的任何 WebElement
> Selenium iframe
。
原因:
加载页面时,默认情况下, Selenium
的焦点位于 顶部窗口
。 顶部窗口
包含其他 iframes
强> 框架集
即可。因此,当我们需要与iframe中的 WebElement
进行交互时,我们必须通过一个切换到相应的 iframe
下面提到的方法:
帧切换方法:
我们可以通过3种方式切换到帧。
按帧名称:
名称
属性,我们可以通过它来切换到它。
示例:
driver.switch_to.frame(iframe_name)
按帧ID :
> ID
$属于我们可以切换到的iframe的属性。
示例:
driver.switch_to.frame(iframe_id)
By Frame Index : 假设页面中有10个框架,我们可以使用索引切换到iframe。 充足: 切换回主框架: 我们可以通过使用 例子: 切换帧的更好方法是诱导 通过 <$ c 通过 <$ c 通过 通过 I am trying to select an element which resides inside an iframe and probably in other iframes. Is it somehow possible to select an element within some (sub-)iframe in (python)selenium without selecting the iframes before? Is there a way to 'loop' over every iframe somehow and to check where to find my element...? And how to do that in the case elements and html stuff and iframes might just about being loaded...? No, it is not possible to interact with any When a page is loaded, We can switch over to frames by 3 ways. By Frame Name : Example: By Frame ID : Example: By Frame Index : Suppose if there are 10 frames in the page, we can switch to the iframe by using the index. Example: Switching back to the Main Frame : We can switch back to the main frame by using Example:
A better way to switch frames will be to induce Through Through Through Through 这篇关于我怎样才能选择一个html元素,不管它在硒中的位置是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
pre $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ driver $
default_content()
或 parent_frame()来切换回主框架。
driver.switch_to.default_content()
driver.switch_to.parent_frame()
切换帧的更好方法:
WebDriverWait
设置为 <$ c expected_conditions
$ c> frame_to_be_available_and_switch_to_it ,如下所示:
WebDriverWait(driver,10).until($ c $) EC.frame_to_ be_available_and_switch_to_it(By.ID,id_of_iframe))
WebDriverWait(driver,10).until($ c $> EC.frame_to_be_available_and_switch_to_it(By.NAME,name_of_iframe))
Frame Xpath
:
WebDriverWait(driver,10)。直到(EC.frame_to_be_available_and_switch_to_it(By.XPATH,xpath_of_iframe))
Frame CSS
:
WebDriverWait ).until(EC.frame_to_be_available_and_switch_to_it(By.CSS_SELECTOR,css_of_iframe))
WebElement
within an iframe
through Selenium
without switching to the respective iframe
.Reason :
Selenium
's focus by default remains on the Top Window
. The Top Window
contains the other iframes
and the framesets
. So when we need to interact with a WebElement
which is within an iframe we have to switch to the respective iframe
through one of the below-mentioned methods :Frame Switching Methods :
Name
attribute of iframe through which we can switch to it.driver.switch_to.frame("iframe_name")
ID
attribute of iframe through which we can switch to it.driver.switch_to.frame("iframe_id")
driver.switch_to.frame(0)
driver.switch_to.frame(1)
default_content()
or parent_frame()
driver.switch_to.default_content()
driver.switch_to.parent_frame()
A Better Approach to Switch Frames:
WebDriverWait
for the availability of the intended frame with expected_conditions
set to frame_to_be_available_and_switch_to_it
as follows :
Frame ID
:WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it(By.ID,"id_of_iframe"))
Frame Name
:WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it(By.NAME,"name_of_iframe"))
Frame Xpath
:WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it(By.XPATH,"xpath_of_iframe"))
Frame CSS
:WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it(By.CSS_SELECTOR,"css_of_iframe"))