Selenium :: WebDriver :: Error :: ElementNotVisibleError:使用Chrome浏览器时调用send_key函数时看不到元素。
Selenium::WebDriver::Error::ElementNotVisibleError: element not visible
我正在并行运行ruby selenium脚本,并在通过send_key将文本输入发送到网页时面临以下问题。
Im using following versions:
(Session info: chrome=43.0.2357.125)
(Driver info: chromedriver=2.15.322448 (52179c1b310fec1797c81ea9a20326839860b7d3),platform=Linux 3.2.0-29-generic x86_64)
以下是我的代码片段:
class MyClass
dir="xyz"
@headless = Headless.new(display: 100, reuse: true, destroy_at_exit: false).start
Selenium::WebDriver::Chrome.driver_path = "/home/ubuntu/chromedriver"
@driver = Selenium::WebDriver.for :chrome, :switches => %W[--ignore-certificate-errors --enable-logging --user-data-dir=#{dir} --disable-popup-blocking --disable-translate --use-fake-ui-for-media-stream --use-file-for-fake-audio-capture=song.wav --use-file-for-fake-video-capture=city_cif_15fps.y4m --use-fake-device-for-media-stream]
@driver.get(@base_url + "/#{$url}/")
wait = Selenium::WebDriver::Wait.new(:timeout => 90) # seconds
wait.until {
@driver.find_element(:id, "join-dialog-join")}
x=@driver.find_element(:id, "join-dialog-input-name")
@driver.execute_script("document.getElementById('join-dialog-input-name').style.visibility='visible';")
@driver.execute_script("document.getElementById('join-dialog-input-name').removeAttribute('hidden');")
#@driver.save_screenshot("Join-display_login_#{DateTime.now.to_s}_1_#{$epname}.png")
#puts x.attribute("innerHTML")
@driver.find_element(:id, "join-dialog-input-name").send_key "#{$epname}"
end
我试过让元素可见,我也尝试删除属性隐藏,如果碰巧是这样的话。 我也尝试使用JS而不是send_keys,但似乎没有帮助。
@driver.execute_script("document.getElementById('join-dialog-input-name').value='Nikhil';")
任何帮助将不胜感激!!。
Xvfb会导致这种情况吗?控制台日志
建议使用以下内容[5245:5245:0626/181715:ERROR:chrome_browser_main_extra_parts_x11.cc(56)] X IO error received (X server probably went away)
[5280:5280:0626/181715:ERROR:x11_util.cc(82)] X IO error received (X server probably went away)
注意:只有当我并行启动脚本时才会出现此问题。
HTML代码段:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" class="snowy " lang="en"><head>
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> <!-- , maximum-scale=1.0 -->
<meta name="robots" content="noindex, nofollow" />
<!-- N.B.: 'IE=edge' must be the first, else it may be ignored -->
<!-- 'IE=edge' forces users out of compatibility/IE7 mode, even when in intranet -->
<!-- 'requiresActiveX=true' alerts users on IE10 metro mode that they need to switch back to IE32 -->
<!-- 'chrome=1' for chromeframe compatibility -->
<meta http-equiv="X-UA-Compatible" content="IE=edge,requiresActiveX=true,chrome=1" />
<!-- <meta http-equiv="X-UA-Compatible" content="IE=Edge" /> -->
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<body class="gradient">
<div id="join-dialog" class="input-text" style="display: none;">
<div class="dialog-header guest-flow">
<div class="text-blue">
<div class="proxima-bold text-25">Welcome.</div>
<div class="proxima-light text-25">Please tell us who you are.</div>
</div>
</div>
<div id="join-dialog-content">
<!-- Guest workflow -->
<div class="guest-flow" style="display: none;">
<div class="field-wrapper">
<input id="join-dialog-input-name" type="text" maxlength="50" placeholder="Enter your Name" rel="Enter your Name" value="" class="applytitle clearfocus" tabindex="1" aria-label="Enter Your Name" /><!-- _cato_ -->
<div id="guest-name-error" class="error" style="display: none;">
Please enter a valid name.
</div>
</div>
<div class="guest-email" style="display: none;">
<div class="field-wrapper">
<input id="join-dialog-input-email" type="text" maxlength="70" value="" class="applytitle clearfocus" tabindex="2" aria-label="Enter your email address" /><!-- _cato_ -->
<div id="guest-email-error" class="error" style="display: none;">
Please enter a valid email address.
</div>
</div>
</div>
</div>
</div>
</div>
</body></html>
答案 0 :(得分:0)
我认为问题是在Chrome的默认视图框中确实看不到元素。 (并且在默认窗口大小中无法显示)
要通过webdriver设置窗口大小,你可以做一些事情,比如browser.manage().window().setSize(1200,1100);
(这是在javascript下。我觉得它在ruby下看起来很相似)
在没有Xvfb的无头模式下使用chrome(自V59起)。另请参阅此帖子https://stackoverflow.com/a/46299332/2718002