3

I'm attempting to use Selenium to run a function within a different frame. Within the Chrome console I can call the function when residing within the context:

enter image description here

However, I cannot call the function from the top level: enter image description here

The code that I have right now is as follows:

from seleniumwire import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()

driver.get("MY_WEBSITE")
frame_id = "main-iframe" 
WebDriverWait(driver, 10).until(expected_conditions.presence_of_element_located((By.ID, """main-iframe""")))

driver.switch_to.frame(driver.find_element_by_id(frame_id))
driver.execute_script("onCaptchaFinished();")

But this returns the following error:

JavascriptException: Message: ReferenceError: onCaptchaFinished is not defined

Any help would be massively appreciated. Note that this question is not similar to earlier questions, as I am specifically attempting to run a Javascript function from within an iframe, something that no questions have gone over.

Menno Van Dijk
  • 863
  • 6
  • 24

1 Answers1

0

You need to set the frame using the window.frame command, in my case i used: window.frame[1].myfunc('arg') and it worked, use the Google Chrome developer tools to get the correct frame and then apply to your code:

driver.execute_script("window.frames[1].MyFunc('arg');")
Vinicius B
  • 162
  • 9