消息:元素不可见

时间:2018-06-18 11:44:32

标签: python-2.7 selenium-webdriver

在Python 2.7上使用selenium在页面上使用send_keys时遇到一些问题。与页面中的其他元素交互是可以的,但不是这个元素:

<div id="request"><textarea id="content" style="display: none;"></textarea><div class="CodeMirror cm-s-default"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 6px; left: 5px;"><textarea autocorrect="off" autocapitalize="off" style="position: absolute; padding: 0px; width: 1000px; height: 1em; outline: none;" tabindex="0"></textarea></div><div class="CodeMirror-hscrollbar" style="left: 0px;"><div style="height: 1px;"></div></div><div class="CodeMirror-vscrollbar"><div style="width: 1px;"></div></div><div class="CodeMirror-scrollbar-filler"></div><div class="CodeMirror-scroll" tabindex="-1" draggable="false"><div style="position: relative; min-height: 100%;"><div class="CodeMirror-sizer" style="min-width: 44.7969px; margin-left: 0px; min-height: 25px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines"><div style="position: relative; outline: none;"><div class="CodeMirror-measure"><pre><span>&nbsp;</span></pre></div><div style="position: relative; z-index: 1; display: none;"><div class="CodeMirror-selected" style="position: absolute; left: 4px; top: 1px; width: 46.7812px; height: 15px;"></div></div><div style=""><pre>&nbsp;</pre><pre style="display: none;">ffffff</pre></div><div class="CodeMirror-cursor" style="left: 4px; top: 1px; height: 15px; visibility: hidden;">&nbsp;</div><div class="CodeMirror-cursor CodeMirror-secondarycursor" style="display: none; visibility: hidden;">&nbsp;</div></div></div></div></div><div style="position: absolute; height: 30px; top: 25px;">&nbsp;</div><div class="CodeMirror-gutters" style="display: none;"></div></div></div></div></div>

我尝试过以下选项:

driver.find_element_by_id('content').click().send_keys("Hello World!")

driver.find_element_by_xpath('//*[@id="ad-request"]/div/div[5]/div/div[1]/div/div/div/div[3]/pre').send_keys("Hello World!")

但它们都会触发ElementNotVisibleException

1 个答案:

答案 0 :(得分:0)

使用action_chains解决:

action.send_keys(keys.Keys.COMMAND+keys.Keys.ALT+'')
action.perform()
time.sleep(3)
action.send_keys("content"+keys.Keys.ENTER)
action.perform()
相关问题