Is It Possible To Emulate The Firefox-console Cd Function And/or Javascript-context-switching In Selenium-webriver?
Solution 1:
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:
- through the console cd command or
- through the drop-down frame-context-switching menu
cd command in Selenium with Python
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 itsDOM
.
Firefox console frame-selection menu in Selenium with Python
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.
Solution 2:
I don't see why it should not work to access elements in a cross-origin frame directly with Selenium / geckodriver.
The following code example that loads a page from Domain A
, switches to the iframe
that holds a page from Domain B
, let me directly access the h1
element and read out its text:
driver.get("https://output.jsbin.com/gayeyanosi/2")
iframe = driver.find_element_by_css_selector("iframe") driver.switch_to.frame(iframe)
heading = driver.find_element_by_css_selector("h1") assert heading.text == "Example Domain"
Support for that is already around for a couple of Firefox and geckodriver releases.
If it still doesn't work for you I would suggest that you file an issue for geckodriver, and attach a trace level log to it. We can then investigate the problem in detail.
Post a Comment for "Is It Possible To Emulate The Firefox-console Cd Function And/or Javascript-context-switching In Selenium-webriver?"