【问题标题】:Run a function inside a Javascript context on Selenium在 Selenium 上的 Javascript 上下文中运行函数
【发布时间】:2020-11-28 06:38:04
【问题描述】:

我正在尝试使用 Selenium 在不同的框架中运行一个函数。在 Chrome 控制台中,我可以在上下文中调用该函数:

但是,我无法从顶层调用该函数:

我现在的代码如下:

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 函数,这没有任何问题。

【问题讨论】:

    标签: python python-3.x selenium


    【解决方案1】:

    您需要使用 window.frame 命令设置框架,在我的情况下,我使用的是:window.frame[1].myfunc('arg') 并且它有效,使用 Google Chrome 开发人员工具来获取正确的框架然后应用于您的代码:

    driver.execute_script("window.frames[1].MyFunc('arg');")
    

    【讨论】:

      猜你喜欢
      • 2016-11-04
      • 2015-07-15
      • 1970-01-01
      • 2018-05-16
      • 1970-01-01
      • 2014-01-20
      • 2019-01-16
      • 2021-12-30
      • 1970-01-01
      相关资源
      最近更新 更多