使用Ruby Selenium Webdriver无法选择单选按钮,因为标签和按钮被隐藏

时间:2016-05-18 16:43:45

标签: ruby selenium firefox selenium-webdriver webdriver

我已经在互联网上仔细研究过,但我不能。

尝试为现有网站创建自动化测试(因此我无法编辑网站css / html / etc.)。 我想在表单上选择一个单选按钮,但是,当我在命令行中运行.rb文件时,我得到“元素当前不可见,因此可能无法与...交互”错误消息。

似乎单选按钮及其标签位于“p”旁边,因此不可见,这意味着我似乎无法点击它。即使我用鼠标悬停在元素上,也不会显示手形指针,显示它在鼠标悬停时不会显示。

我已经尝试了wait(),但网站似乎没有等待加载才能显示按钮。 没有其他元素包含相同的ID,因此没有重复项。当我实际选择按钮时,似乎才变得活跃。 网站上的单选按钮的代码如下(我已经更改了一些名称导致长名称/ ID等):

<div class="panel-body">
    <p>Where the description is.</p>
    <p>Please select from the following options</p>
    <input id="MainContent_CreateProposalWizard_Contacted" type="radio" name="ctl00$MainContent$CreateProposalWizard$ContactedAnswer" value="Contacted">
    <label for="MainContent_CreateProposalWizard_Contacted"> Yes</label>
    <p>Contacted</p>
    <input id="MainContent_CreateProposalWizard_NotContacted" type="radio" name="ctl00$MainContent$CreateProposalWizard$ContactedAnswer" value="NotContacted"><label for="MainContent_CreateProposalWizard_NotContacted"> No</label>
    <p>Not Contacted</p>
    <input id="MainContent_CreateProposalWizard_NotApplicable" type="radio" name="ctl00$MainContent$CreateProposalWizard$ContactedAnswer" value="NotApplicable">
    <label for="MainContent_CreateProposalWizard_NotApplicable"> Not Applicable</label>
    <p>Not Contacted</p>

    <input type="submit" name="ctl00$MainContent$CreateProposalWizard$btnPreviousNoExact2" value="Previous" id="MainContent_CreateProposalWizard_btnPreviousNoExact2" class="btn btn-sm btn-success strong">
    <input type="submit" name="ctl00$MainContent$CreateProposalWizard$btnContinueNoExact2" value="Continue" id="MainContent_CreateProposalWizard_btnContinueNoExact2" class="btn btn-sm btn-success strong">
</div>

似乎应用于标签的CSS:

background-image: url("....PNG")
background-repeat: no-repeat
box-sizing: border-box
color: rgb (99, 105, 115)
cursor: default
display: inline-block
font-family: arial
font-size: 12px
font-weight: normal
line-height: 17px
margin-bottom: 5px
margin-top: 3px
padding: 0px 17px 0px 17px
text-align: left
white-space: normal
-webkit-user-select: none

输入的CSS:

...
-webkit-appearance: radio
-webkit-rtl-ordering: logical
-webkit-user-select: text

我正在使用Ruby,但我没有使用python,javascript,java或任何其他语言只是Ruby selenium webdriver。我的代码如下:

require "selenium-webdriver"
browser = Selenium::WebDriver.for :firefox
browser.get "http://webiste.com"
browser.find_element(id: "MainContent_CreateProposalWizard_NotApplicable").click

我收到的错误代码:

[remote server] file:///C:/Users/SOPHIE~1.MOR/AppData/Local/Temp/webdriver-profile20160523-14328-tmuf3p/extensions/fxdriver@googlecode.com/components/command-processor.js:10092:in `fxdriver.preconditions.visible': Element is not currently visible and so may not be interacted with Selenium::WebDriver::Error::ElementNotVisibleError)
from [remote server] file:///C:/Users/SOPHIE~1.MOR/AppData/Local/Temp/webdriver-profile20160523-14328-tmuf3p/extensions/fxdriver@googlecode.com/components/command-processor.js:12644:in `DelayedCommand.prototype.checkPreconditions_'
    from [remote server] file:///C:/Users/SOPHIE~1.MOR/AppData/Local/Temp/webdriver-profile20160523-14328-tmuf3p/extensions/fxdriver@googlecode.com/components/command-processor.js:12661:in `DelayedCommand.prototype.executeInternal_/h'
    from [remote server] file:///C:/Users/SOPHIE~1.MOR/AppData/Local/Temp/webdriver-profile20160523-14328-tmuf3p/extensions/fxdriver@googlecode.com/components/command-processor.js:12666:in `DelayedCommand.prototype.executeInternal_'
    from [remote server] file:///C:/Users/SOPHIE~1.MOR/AppData/Local/Temp/webdriver-profile20160523-14328-tmuf3p/extensions/fxdriver@googlecode.com/components/command-processor.js:12608:in `DelayedCommand.prototype.execute/<'
    from C:/Ruby22/lib/ruby/gems/2.2.0/gems/selenium-webdriver-2.53.0/lib/selenium/webdriver/remote/response.rb:70:in `assert_ok'
    from C:/Ruby22/lib/ruby/gems/2.2.0/gems/selenium-webdriver-2.53.0/lib/selenium/webdriver/remote/response.rb:34:in `initialize'
    from C:/Ruby22/lib/ruby/gems/2.2.0/gems/selenium-webdriver-2.53.0/lib/selenium/webdriver/remote/http/common.rb:78:in `new'
    from C:/Ruby22/lib/ruby/gems/2.2.0/gems/selenium-webdriver-2.53.0/lib/selenium/webdriver/remote/http/common.rb:78:in `create_response'
    from C:/Ruby22/lib/ruby/gems/2.2.0/gems/selenium-webdriver-2.53.0/lib/selenium/webdriver/remote/http/default.rb:90:in `request'
    from C:/Ruby22/lib/ruby/gems/2.2.0/gems/selenium-webdriver-2.53.0/lib/selenium/webdriver/remote/http/common.rb:59:in `call'
    from C:/Ruby22/lib/ruby/gems/2.2.0/gems/selenium-webdriver-2.53.0/lib/selenium/webdriver/remote/bridge.rb:649:in `raw_execute'
    from C:/Ruby22/lib/ruby/gems/2.2.0/gems/selenium-webdriver-2.53.0/lib/selenium/webdriver/remote/bridge.rb:627:in `execute'
    from C:/Ruby22/lib/ruby/gems/2.2.0/gems/selenium-webdriver-2.53.0/lib/selenium/webdriver/remote/bridge.rb:389:in `clickElement'
    from C:/Ruby22/lib/ruby/gems/2.2.0/gems/selenium-webdriver-2.53.0/lib/selenium/webdriver/common/element.rb:73:in `click'
    from new_proposal.rb:26:in `<main>'

任何有关如何选择此单选按钮的帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

在尝试查找元素之前,请确保打开页面。

我使用node:

为您的示例服务
<style>
input {
    -webkit-appearance: radio
    -webkit-rtl-ordering: logical
    -webkit-user-select: text
}

label {
    background-image: url("....PNG");
    background-repeat: no-repeat;
    box-sizing: border-box;
    color: rgb (99, 105, 115);
    cursor: default;
    display: inline-block;
    font-family: arial;
    font-size: 12px;
    font-weight: normal;
    line-height: 17px;
     margin-bottom: 5px;
     margin-top: 3px;
     padding: 0px 17px 0px 17px;
     text-align: left;
     white-space: normal;
    -webkit-user-select: none;
}
</style>
<div class="panel-body">
    <p>Where the description is.</p>
    <p>Please select from the following options</p>
    <input id="MainContent_CreateProposalWizard_Contacted" type="radio" name="ctl00$MainContent$CreateProposalWizard$ContactedAnswer" value="Contacted">
    <label for="MainContent_CreateProposalWizard_Contacted"> Yes</label>
    <p>Contacted</p>
    <input id="MainContent_CreateProposalWizard_NotContacted" type="radio" name="ctl00$MainContent$CreateProposalWizard$ContactedAnswer" value="NotContacted"><label for="MainContent_CreateProposalWizard_NotContacted"> No</label>
    <p>Not Contacted</p>
    <input id="MainContent_CreateProposalWizard_NotApplicable" type="radio" name="ctl00$MainContent$CreateProposalWizard$ContactedAnswer" value="NotApplicable">
    <label for="MainContent_CreateProposalWizard_NotApplicable"> Not Applicable</label>
    <p>Not Contacted</p>

    <input type="submit" name="ctl00$MainContent$CreateProposalWizard$btnPreviousNoExact2" value="Previous" id="MainContent_CreateProposalWizard_btnPreviousNoExact2" class="btn btn-sm btn-success strong">
    <input type="submit" name="ctl00$MainContent$CreateProposalWizard$btnContinueNoExact2" value="Continue" id="MainContent_CreateProposalWizard_btnContinueNoExact2" class="btn btn-sm btn-success strong">
</div>

并且能够使用以下方式选择单选按钮:

require "selenium-webdriver"
browser = Selenium::WebDriver.for :firefox
browser.get "http://localhost:8088/test.html"

browser.find_element(id: "MainContent_CreateProposalWizard_NotApplicable").click

如果仍然无效,请更新您的示例。很可能你只有一个糟糕的选择器。