Selenium::WebDriver::Error::ElementNotVisibleError:在使用Chrome

Selenium::WebDriver::Error::ElementNotVisibleError: element not visible while calling send_key function when Chrome browser is used

本文关键字:Chrome ElementNotVisibleError WebDriver Error Selenium      更新时间:2023-09-26

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>

我认为问题是Element在Chrome的默认视图框中确实不可见。(在默认窗口大小下无法显示)

要通过webdriver设置窗口大小,你可以做一些类似browser.manage().window().setSize(1200,1100);的事情(这是在javascript下。我认为它在ruby下看起来很相似)

当您在没有Xvfb的无头模式下使用chrome(自V59以来)时。另请参阅此帖子https://stackoverflow.com/a/46299332/2718002