当前位置: 首页 > 知识库问答 >
问题:

使用Ruby Selenium Webdriver不能选择单选按钮,因为标签和按钮是隐藏的

公冶昆杰
2023-03-14

我努力在网上寻找答案,但是我做不到。

尝试为现有网站创建自动测试(因此我无法编辑网站css/html/等)。我想在表单上选择一个单选按钮,但是,当我在命令行中运行.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个答案

傅增
2023-03-14

在尝试定位元素之前,请确保您正在打开页面。

我使用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

如果仍然不起作用,请更新您的示例。您很可能只是有一个糟糕的选择器。

 类似资料:
  • 问题内容: 我只是注意到IE7中有一个奇怪的行为。 我有带有相关标签的单选按钮,如下所示: 单选按钮通过CSS隐藏,显示:无或可见性:隐藏(不问) 问题是-当我单击IE7中的标签(尚未查看其他IE版本)时,实际上未选中关联的单选按钮。我通过jquery确认了这一点- 标签click事件被触发,但单选按钮click事件未触发。表单过帐还确认选中的单选按钮没有更改。 这在firefox中可以正常使用,

  • 问题内容: 是否可以将css(3)样式应用于选中的单选按钮的标签? 我有以下标记: 我希望的是 会做某事,但是可惜它没有(我预期的那样)。 是否有一个选择器可以实现这种功能?如果有帮助,您可以将divs围起来,但是最好的解决方案是使用标签“ for”属性。 应该注意的是,我能够为我的应用程序指定浏览器,所以请最好使用css3等类。 问题答案: 尝试符号:它是相邻的同级组合器。它结合了两个具有相同父

  • 我已经搜索了前面的一些问题,并没有能够纠正--我是一个完全的新手,所以请原谅我的无知...尝试使用以下方法选择页面上的第三个“单选”按钮: 结果是: “消息:没有此类元素:找不到元素:{”method“:”XPath“,”Selector“:”//[@id=“Wizard_Tabs”]/div/div[1]/div/ul/li[3]/input“}”

  • 我是Selenium IDE的新手,在这里需要选择单选按钮的帮助。就我而言,我正在尝试为由单选按钮组成的特定表单生成一个测试用例。当我运行命令以单独选择其中一个单选按钮时,该函数可以工作,但如果我运行整个测试用例,则单选按钮不会被选中并给出找不到Element Id的错误。这是我的html: 我的IDE命令:单击Target: id=ProjectSolutionsProject0。我尝试了ver

  • 问题内容: 我正在尝试根据列表创建多个复选框,但是看起来好像在搞砸按钮的 命令调用 和 变量方面 。 我的代码是: 到目前为止,该测试是针对在选中复选框的情况下将课程打印在控制台上的,但是仅对第二个按钮“ CSE 4343”有效。当我与按钮“ CSE 4444”交互时,什么都不会打印。 同样,无论我单击按钮“ CSE 4343”还是按钮“ CSE 4444”,“ place”值的onClick始终

  • 这是一个 列表 拓展,可以用来创建单选和复选按钮组。 复选按钮组 <div class="list-block"> <ul> <!-- Single chekbox item --> <li> <label class="label-checkbox item-content"> <!-- Checked by default -->