title: Run a function inside a Javascript context on Selenium
body: <p>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:</p>
  <p><a href="https://i.stack.imgur.com/xH3c6.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/xH3c6.png" alt="enter image description here" /></a></p>
  <p>However, I cannot call the function from the top level:
  <a href="https://i.stack.imgur.com/Owwd8.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Owwd8.png" alt="enter image description here" /></a></p>
  <p>The code that I have right now is as follows:</p>
  <pre><code>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(&quot;MY_WEBSITE&quot;)
  frame_id = &quot;main-iframe&quot; 
  WebDriverWait(driver, 10).until(expected_conditions.presence_of_element_located((By.ID, &quot;&quot;&quot;main-iframe&quot;&quot;&quot;)))
  
  driver.switch_to.frame(driver.find_element_by_id(frame_id))
  driver.execute_script(&quot;onCaptchaFinished();&quot;)
  </code></pre>
  <p>But this returns the following error:</p>
  <pre><code>JavascriptException: Message: ReferenceError: onCaptchaFinished is not defined
  </code></pre>
  <p>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.</p>