Python 在Selenium上的Javascript上下文中运行函数
我正在尝试使用Selenium在不同的框架内运行函数。在Chrome控制台中,我可以在上下文中调用该函数: 但是,我无法从顶层调用该函数: 我现在掌握的代码如下:Python 在Selenium上的Javascript上下文中运行函数,python,python-3.x,selenium,Python,Python 3.x,Selenium,我正在尝试使用Selenium在不同的框架内运行函数。在Chrome控制台中,我可以在上下文中调用该函数: 但是,我无法从顶层调用该函数: 我现在掌握的代码如下: from seleniumwire import webdriver from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions from selenium.we
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();")
但这将返回以下错误:
JavascriptException: Message: ReferenceError: onCaptchaFinished is not defined
任何帮助都将不胜感激。请注意,这个问题与前面的问题不同,因为我特别尝试从iframe中运行Javascript函数,这是没有问题的。您需要使用window.frame命令来设置框架,在我的例子中,我使用了:window.frame[1].myfunc('arg'),它可以工作,使用Google Chrome developer工具获得正确的框架,然后应用于您的代码:
driver.execute_script("window.frames[1].MyFunc('arg');")