bgi*_*ton 7 ruby webdriver watir-webdriver selenium-webdriver
因此,我们在页面中有以下代码:
<div class="toggle-wrapper">
<input id="HasRegistration_true" class="registration_required toggle" type="radio" value="True" name="HasRegistration" data-val-required="The HasRegistration field is required." data-val="true">
<label for="HasRegistration_true" class="">On</label>
<input id="HasRegistration_false" class="registration_required toggle" type="radio" value="False" name="HasRegistration" checked="checked">
<label class="checked" for="HasRegistration_false">Off</label>
</div>
Run Code Online (Sandbox Code Playgroud)
这是2个单选按钮.'开和关'.'关'是默认值.
使用Watir-webdriver和Ruby,我们想要选择'On'单选按钮.我们这样做:
browser.radio(:id => "HasRegistration_true").set
Run Code Online (Sandbox Code Playgroud)
但是这样做会导致以下错误:
`WebElement.clickElement': Element cannot be scrolled into view:[object HTMLInputElement] (Selenium::WebDriver::Error::MoveTargetOutOfBoundsError)
Run Code Online (Sandbox Code Playgroud)
我们知道Selenium 2将页面滚动到元素,因此尝试向下滚动是没用的.我们总是使用最新版本的watir-webdriver和ruby.
由于我们是QA工程师,因此我们无法更改页面的HTML.
首先尝试使用 XPATH 定位元素:
browser.element(:xpath, "//input[@id='HasRegistration_true']").click
Run Code Online (Sandbox Code Playgroud)
或者
或者,如果您尝试定位它是一个隐藏元素,那么您最好使用 CSS。下载适用于 Firefox 的 firebug 插件并复制元素的 CSS 路径。
它应该是这样的:
browser.element(:css => "the CSS path you have copied from Firebug").click
Run Code Online (Sandbox Code Playgroud)
两者之一应该为你做这件事!
祝你好运!