我有一个这样的页面:
<div class="CodeMirror">
<div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 6.2px; left: 182.2px;">
<textarea autocapitalize="off" autocorrect="off" style="position: absolute; padding: 0px; width: 1px; height: 1em;" wrap="off"></textarea>
</div>
<div style="display: none; height: 106px;" class="CodeMirror-scrollbar"> <div style="height: 114px;" class="CodeMirror-scrollbar-inner"></div></div>
<div> ...
...
..
</div>
<pre style="top: 39px; left: 0px;" class="CodeMirror-cursor"> </pre>
<div style="">
<pre>asdf</pre>
<pre>asdfasdf</pre> It's weird the textarea content is here
<pre>asdfasdf</pre>
<pre> </pre>
</div>
...
...
我使用selenium进行选择,但是获取两个元素并将密钥发送给其中一个元素,它会抛出Selenium::WebDriver::Error::ElementNotVisibleError: Element is not currently visible and so may not be interacted with
这是我的操作命令:
browser.browser.public_send(:textareas, :xpath => "//div[@class='CodeMirror']//textarea[@wrap='off']" )[0].html
=> "<textarea autocapitalize=\"off\" autocorrect=\"off\" style=\"position: absolute; padding: 0px; width: 1px; height: 1em;\" wrap=\"off\"></textarea>"
[58] pry(#<CucuShift::DefaultWorld>)> browser.browser.public_send(:textareas, :xpath => "//div[@class='CodeMirror']//textarea[@wrap='off']" )[1].html
=> "<textarea autocapitalize=\"off\" autocorrect=\"off\" style=\"position: absolute; padding: 0px; width: 1px; height: 1em;\" wrap=\"off\"></textarea>"
browser.browser.public_send(:textareas, :xpath => "//div[@class='CodeMirror']/descendant::textarea[@autocorrect='off']" )[1].click
Selenium::WebDriver::Error::ElementNotVisibleError: Element is not currently visible and so may not be interacted with
from [remote server] file:///tmp/webdriver-profile20160801-26332-bu7605/extensions/fxdriver@googlecode.com/components/command-processor.js:10092:in `fxdriver.preconditions.visible'
browser.browser.public_send(:textareas, :xpath => "//div[@class='CodeMirror']/descendant::textarea[@autocorrect='off']" )[1].send_keys '1234'
Selenium::WebDriver::Error::ElementNotVisibleError: Element is not currently visible and so may not be interacted with
from [remote server] file:///tmp/webdriver-profile20160801-26332-bu7605/extensions/fxdriver@googlecode.com/components/command-processor.js:10092:in `fxdriver.preconditions.visible'
最奇怪的是那些元素html是相同的。
答案 0 :(得分:0)
您无法访问textarea的原因是您的textarea的父div具有属性&#34; overflow:hidden&#34;。 因此,您的textarea是隐形的。如果删除&#34;溢出:隐藏&#34;您的代码属性,textarea将可见,您可以使用Selenium和手动访问它。
审稿人注意:事实上,问题本身并不是那么清楚。我试图依靠标题回答它:用户想要与网络元素进行交互,我的回答提供了解决方案。