Python分裂器在iframe中填写文本

时间:2017-07-16 10:41:09

标签: python selenium crm splinter

我正在使用分裂器自动化autoraptor CRM,一切都运行顺畅,直到编写消息为止。 CRM的电子邮件编辑器位于iframe内。我可以找到带有分裂的iframe但我无法在编辑器中编写任何内容。

这是html

<div id="cke_1_contents" class="cke_contents cke_reset" role="presentation" style="height: 200px;"><span id="cke_67" class="cke_voice_label">Press ALT 0 for help</span><iframe src="" frameborder="0" class="cke_wysiwyg_frame cke_reset" title="Rich Text Editor, email_body" aria-describedby="cke_67" tabindex="0" allowtransparency="true" style="width: 100%; height: 100%;"></iframe></div>

我已阅读Frames, alerts and prompts

这是Python

#!/usr/bin/env python 

from splinter import Browser
import urlparse
import time

# Creds
username = "lumberjack@imok.com"
password = "spamspamspam..."

login_url = 'https://ar.autoraptor.com/login'

browser = Browser('chrome')

#help(browser)

# Go to login page
browser.visit(login_url)

# Fill in Username
browser.find_by_name('login').fill(username)

# Fill in 
browser.find_by_name('password').fill(password)

# Click the login Button
browser.find_by_value('Log In').click()

# Navigate to upsheets
#browser.find_by_xpath('//*[@id="nav"]/li[4]/a').click()

# Navigate to least updated upsheets
url = "https://ar.autoraptor.com/upsheets?filter=active&sort=updated_at%5B1%5D"
browser.visit(url)

# Click on notifications
browser.find_by_xpath('//*[@id="nav"]/li[8]/div[1]').click()
time.sleep(5)

# Click on email
browser.find_by_xpath('//*[@id="notification-tab-incoming_email"]/div').click()
time.sleep(5)

# Read email
browser.find_by_text("Email from ").click()

# Send email
browser.find_by_id('newemailplaceholder').click()
time.sleep(5)
browser.find_by_id("cke_1_contents").fill("ftw")
#browser.quit()

这是追溯

Traceback (most recent call last):
  File "testbot.py", line 50, in <module>
    browser.find_by_id("cke_1_contents").find_by_tag("iframe").fill("foo")
  File "/home/ricky/.local/lib/python2.7/site-packages/splinter/driver/webdriver/__init__.py", line 529, in fill
    self.value = value
  File "/home/ricky/.local/lib/python2.7/site-packages/splinter/driver/webdriver/__init__.py", line 511, in _set_value
    self._element.clear()
  File "/home/ricky/.local/lib/python2.7/site-packages/selenium/webdriver/remote/webelement.py", line 92, in clear
    self._execute(Command.CLEAR_ELEMENT)
  File "/home/ricky/.local/lib/python2.7/site-packages/selenium/webdriver/remote/webelement.py", line 493, in _execute
    return self._parent.execute(command, params)
  File "/home/ricky/.local/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 256, in execute
    self.error_handler.check_response(response)
  File "/home/ricky/.local/lib/python2.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 194, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.InvalidElementStateException: Message: invalid element state: Element must be user-editable in order to clear it.
  (Session info: chrome=59.0.3071.115)
  (Driver info: chromedriver=2.30.477691 (6ee44a7247c639c0703f291d320bdf05c1531b57),platform=Linux 4.10.0-26-generic x86_64)

1 个答案:

答案 0 :(得分:0)

例外的原因是即使HTML5属性contenteditable="true"也无法输入IFrame。如果您对HTML有任何控制权,请尝试以下操作,如果上面包含上述属性,您将看到可以实际编辑段落。如果HTML不受控制,您可以做的是在中找到元素(如果有的话),允许您与之交互。如果Iframe中没有元素,那么您将不得不求助于鼠标/键盘控制器与Iframe交互的方式,这意味着物理移动鼠标并模拟击键。

<div id="cke_1_contents" class="cke_contents cke_reset" role="presentation" style="height: 200px;">
    <span id="cke_67" class="cke_voice_label">Press ALT 0 for help</span>
    <iframe src="" frameborder="0" class="cke_wysiwyg_frame cke_reset" title="Rich Text Editor, email_body" aria-describedby="cke_67" tabindex="0" allowtransparency="true" style="width: 100%; height: 100%;"></iframe>
        <p contenteditable="true">Begin with some text</p>
</div>