是否可以在selenium-webriver中模拟Firefox-console cd功能和/或javascript-context-switching? [英] Is it possible to emulate the Firefox-console cd function and/or javascript-context-switching in selenium-webriver?

查看:69
本文介绍了是否可以在selenium-webriver中模拟Firefox-console cd功能和/或javascript-context-switching?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Firefox当前 Web控制台(版本 80.0我键入的.1 支持通过 cd 函数(尽管

The Firefox Web Console currently (version 80.0.1 as I type this) supports Javascript-context-switching to an iframe through a cd function (albeit set to be removed), as in

var ifr = document.getElementById('frame_id');
cd(ifr);

可以实现相同的目标

  • by selecting a different Javascript context from a drop-down menu in the upper-right corner of the Web Console, as mentioned in that bug;
  • similarly in Chrome DevTools (see also an older SO exchange on this).

问题:

使用 selenium-webdriver ?

背景:

我有一个页面正在加载我无法控制的跨域iframe,并且想访问该iframe下的 DOM 元素.我可以在Web控制台中执行此操作,但通过Selenium(特别是 node + selenium-webdriver 包)无法成功完成操作:

I have a page loading a cross-origin iframe I have no control over, and would like to access DOM elements under that iframe. I can do this in the Web Console, but I have been unsuccessful in doing so via Selenium (specifically, node + the selenium-webdriver package):

选择相关的iframe,然后使用切换到它

Selecting the relevant iframe and then switching to it with

ifr = driver.findElement(By.id('frame_id'));
driver.switchTo().frame(ifr);

使我可以访问与在 Web控制台中看到的不同的 DOM :在 Selenium 中,我无权访问按钮当我直接与浏览器进行交互时,可以在浏览器中找到可用的表格,表格等.

gives me access to a different DOM than what I see in the Web Console: in Selenium I do not have access to the buttons, forms, etc. available in the browser when I interact with the latter directly.

推荐答案

我相信我现在已经掌握了这一点.答案是肯定的:

I believe I'm getting the hang of this now. The answer is affirmative:

一个人可以通过 Selenium 访问所有控制台功能(包括 cd 命令,按钮/菜单等).

One can access all of the console functionality (including the cd command, buttons / menus, etc.) through Selenium.

最终使我感到困惑的是对其他相关问题我发布了.我将描述在 Firefox 中进行此操作的两种可能的方法,并与直接使用浏览器时可以访问(可能是跨域的)iframe的两种方法相匹配:

What ultimately got me unstuck was the first comment on this other related question I posted. I will describe two possible ways to go about this in Firefox, matching the two ways one can access a (possibly cross-origin) iframe when working directly with the browser:

脚本:


from selenium.webdriver import Firefox, DesiredCapabilities, FirefoxProfile
from selenium.webdriver.common.by import By
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys

import time
import traceback

options = Options()

webdriver = Firefox(options=options)
webdriver.get(<url-that-embeds-frame>)

try:
    time.sleep(3)
    with webdriver.context(webdriver.CONTEXT_CHROME):

        console = webdriver.find_element(By.ID, "tabbrowser-tabs")
        console.send_keys(Keys.LEFT_CONTROL + Keys.LEFT_SHIFT + 'k')
        time.sleep(3)        

        scrpt = 'ifr = document.getElementById("duo_iframe"); cd(ifr); '
        console.send_keys(scrpt + Keys.ENTER)     
        
except Exception as error:
    traceback.print_exc()

这将

  • 导航到嵌入框架的网址
  • 打开控制台
  • 在该控制台中,使用Javascript定位框架并对其进行 cd 切换到其 DOM .

类似于以上内容:

from selenium.webdriver import Firefox, DesiredCapabilities, FirefoxProfile
from selenium.webdriver.common.by import By
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys

import time
import traceback

options = Options()

webdriver = Firefox(options=options)
webdriver.get(<url-that-embeds-frame>)

try:
    time.sleep(3)
    with webdriver.context(webdriver.CONTEXT_CHROME):

        console = webdriver.find_element(By.ID, "tabbrowser-tabs")
        console.send_keys(Keys.LEFT_CONTROL + Keys.LEFT_SHIFT + 'k')
        time.sleep(3)        
        
        ifr = webdriver.find_element_by_class_name("devtools-toolbox-bottom-iframe")
        webdriver.switch_to.frame(ifr)
        
        btn = webdriver.find_element_by_id("command-button-frames")         
        btn.click()               
        
except Exception as error:
    traceback.print_exc()

这将是

  • 导航到嵌入框架的网址
  • 打开Web控制台
  • 单击该控制台右上角的框架选择菜单

大概也可以在选项等之间循环,但是我还没有这样做.

One can presumably also cycle through the options, etc., but I have not done that.

这篇关于是否可以在selenium-webriver中模拟Firefox-console cd功能和/或javascript-context-switching?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆