是否可以在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?
问题描述
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);
可以实现相同的目标
- 如该错误所述,通过从Web控制台右上角的下拉菜单中选择一个不同的Javascript上下文;
- 在 Chrome DevTools 中也类似(另请参见一个旧版SO交换).
- 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:
- 通过控制台 cd命令或
- 通过下拉菜单框架-上下文切换菜单
脚本:
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屋!