简体   繁体   English

watir-webdriver找不到或填写text_field

[英]watir-webdriver not finding or filling in text_field

I have a watir-webserver Ruby script I am developing, and I am having it first prompt the user for a username and password (with Ruby-Tk). 我有一个正在开发的watir-webserver Ruby脚本,我首先让它提示用户输入用户名和密码(使用Ruby-Tk)。

In any case, I can open the page with this code: 无论如何,我可以使用以下代码打开页面:

b = Watir::Browser.start 'https://connect.mypage.com/Home'

After that, I call the following: 之后,我将其称为:

t1=b.text_field(:id => 'ctl00_cphBody_txtUserName').when_present.set @entry1.textvariable

Nothing is filled in. 什么都没填。

I have tried hard coding a name in for ctl00_cphBody_txtUsername, but that did not work either. 我已经尝试过为ctl00_cphBody_txtUsername硬编码一个名称,但是那也不起作用。

Using the Inspector in Firefox, the field is constructed as following: 在Firefox中使用Inspector,该字段的结构如下:

<input name="ctl00$cphBody$txtUsername" id="ctl00_cphBody_txtUsername" class="firstFocus" maxlength="50" type="text">

The class of firstFocus is: firstFocus的类别是:

<script type="text/javascript">
        $(document).ready(function () {
            jQuery('.firstFocus').focus();
        });
    </script>

If I start the browser and open the page like below, I have no problems locating those two text_field's: 如果启动浏览器并按如下所示打开页面,则找到这两个text_field的对象将没有问题:

browser =  Watir::Browser.new
browser.goto 'https://connect.mypage.com/Home'

#Then, the first item actually has a class, so I use that
t1=browser.text_field :class => 'firstFocus'
t1.set @entry1.textvariable

The problem is that the locator is case-sensitive. 问题是定位器区分大小写。

Notice that the id attribute value is "ctl00_cphBody_txtUsername" but the locator being used is "ctl00_cphBody_txtUserName". 请注意,id属性值为“ ctl00_cphBody_txtUsername”,但所使用的定位符为“ ctl00_cphBody_txtUserName”。 Due to the "N" not matching the "n", the element is never found, which is why the when_present times out. 由于“ N”与“ n”不匹配,因此永远找不到该元素,这就是when_present超时的原因。

Correcting the id in the locator will fix the issue: 更正定位器中的ID将解决此问题:

t1=b.text_field(:id => 'ctl00_cphBody_txtUsername').when_present.set @entry1.textvariable

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM