I believe I'm getting the hang of this now. The answer is affirmative:
One can access all of the console functionality (including the cd
command, buttons / menus, etc.) through Selenium
.
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:
A script:
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()
This will
- navigate to the url that embeds the frame
- open the console
- in that console, use Javascript to locate the frame and
cd
to it, switching to its DOM
.
Similar to the above:
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()
What this will do is
- navigate to the url that embeds the frame
- open the Web Console
- click the frame-selection menu in the top right corner of that console
One can presumably also cycle through the options, etc., but I have not done that.