Javascript 在iframe-Selenium Python中单击元素

Javascript 在iframe-Selenium Python中单击元素,javascript,python,html,selenium,iframe,Javascript,Python,Html,Selenium,Iframe,我的网页中嵌入了一个iframe,我正在使用Selenium自动化测试: <iframe class="wysihtml5-sandbox" security="restricted" allowtransparency="true" frameborder="0" width="0" height="0" marginwidth="0" marginheight="0" style="display: block; background-color: rgb(255, 255, 255)

我的网页中嵌入了一个
iframe
,我正在使用Selenium自动化测试:

<iframe class="wysihtml5-sandbox" security="restricted" allowtransparency="true" frameborder="0" width="0" height="0" marginwidth="0" marginheight="0" style="display: block; background-color: rgb(255, 255, 255); border-collapse: separate; border-color: rgb(163, 192, 216); border-style: solid; border-width: 1px; clear: none; float: none; margin: 0px; outline: rgb(0, 0, 0) none 0px; outline-offset: 0px; padding: 6px 12px; position: static; top: auto; left: auto; right: auto; bottom: auto; z-index: auto; vertical-align: baseline; text-align: start; box-shadow: rgba(0, 0, 0, 0.043) 0px 0.595067px 0.595067px 0px inset; border-radius: 4px; width: 100%; height: 434px;"></iframe>
我编写了以下Selenium automation将文本输入到该正文中,该正文实际上显示为文本框:

iframes = wait.until(EC.frame_to_be_available_and_switch_to_it((By.CLASS_NAME, "wysihtml5-sandbox")))
if iframes:
    try:
        body = browser.find_elements_by_tag_name("body")
        for b in body:
            b.clear()
            b.send_keys(text)
        time.sleep(2)
        # switch back to the original content
        browser.switch_to.default_content()
    except Exception as e:
       print(e)
问题是每当我输入一个文本(“foobar”)并保存时,文本都不会保存。我注意到,如果我点击主体所代表的输入框,那么
文本将被保存,否则不会发生。我想,无论是谁编写了此页面的代码,都希望用户单击并键入输入框(
body
),以便在
iframe
关闭时保存。
我如何才能模拟单击
iframe
输入框以修复我的自动化中的此错误的相同效果?

您可以尝试以下选项

选项1: 在输入文本/清除文本之前,单击body元素

b.click()
# use javascript click(), if normal click not worked
driver.execute_script("arguments[0].click()",b)
选项2: 在正文中输入数据后,触发更改或相关事件

driver.execute_script("arguments[0].dispatchEvent(new Event('onchange', {'bubbles': true,'cancelable': true}));",b)
您可以尝试单独或组合使用这两个选项


参考此项了解与body元素关联的事件。

您可以尝试以下选项

选项1: 在输入文本/清除文本之前,单击body元素

b.click()
# use javascript click(), if normal click not worked
driver.execute_script("arguments[0].click()",b)
选项2: 在正文中输入数据后,触发更改或相关事件

driver.execute_script("arguments[0].dispatchEvent(new Event('onchange', {'bubbles': true,'cancelable': true}));",b)
您可以尝试单独或组合使用这两个选项

请参阅此项以了解与主体元素关联的事件。

帧\u到\u可用\u和\u切换到\u it() 检查给定帧是否可切换到的期望值。如果帧可用,它会将给定的驱动程序切换到指定帧。此方法不返回任何内容。因此,无论执行以下代码行的成功/失败状态如何:

iframes = wait.until(EC.frame_to_be_available_and_switch_to_it((By.CLASS_NAME, "wysihtml5-sandbox")))
iframes
将始终为NULL,如果iframes:
将不执行
中的代码块

您的手动干预可能是帮助驾驶员在所需元素内获得焦点,从而键入文本。您需要做一个小的更改,您的有效代码块将是:

wait.until(EC.frame_to_be_available_and_switch_to_it((By.CLASS_NAME, "wysihtml5-sandbox")))
try:
    body = browser.find_elements_by_tag_name("body")
    for b in body:
        b.clear()
        b.send_keys(text)
    time.sleep(2)
    # switch back to the original content
    browser.switch_to.default_content()
except Exception as e:
    print(e)
帧\u到\u可用\u和\u切换\u到\u it() 检查给定帧是否可切换到的期望值。如果帧可用,它会将给定的驱动程序切换到指定帧。此方法不返回任何内容。因此,无论执行以下代码行的成功/失败状态如何:

iframes = wait.until(EC.frame_to_be_available_and_switch_to_it((By.CLASS_NAME, "wysihtml5-sandbox")))
iframes
将始终为NULL,如果iframes:
将不执行
中的代码块

您的手动干预可能是帮助驾驶员在所需元素内获得焦点,从而键入文本。您需要做一个小的更改,您的有效代码块将是:

wait.until(EC.frame_to_be_available_and_switch_to_it((By.CLASS_NAME, "wysihtml5-sandbox")))
try:
    body = browser.find_elements_by_tag_name("body")
    for b in body:
        b.clear()
        b.send_keys(text)
    time.sleep(2)
    # switch back to the original content
    browser.switch_to.default_content()
except Exception as e:
    print(e)

你看到例外了吗?@DebanjanB没有例外……我已经发布了一个答案。请查看并告诉我状态。@DebanjanB您的解决方案无效。您看到异常了吗?@DebanjanB没有异常……我已经发布了一个答案。请检查并让我知道状态。@DebanjanB您的解决方案无效。