简体   繁体   English

如何在Watir中为变量设置标签

[英]How to set a label to a variable in Watir

I am trying to set some html contents to a variable so i can perform some if statements. 我试图将一些html内容设置为一个变量,以便我可以执行一些if语句。 But I get this instead: 但是我得到了这个:

  Fail #<Watir::Browser:0x00000004440b98>

It looks like my variable isnt set to text i want to set. 看来我的变量未设置为我要设置的文本。

my html: 我的html:

<label class="col-lg-12 control-label ng-binding" ng-show="productionReport.Status == 2 &amp;&amp; productionReport.ReadyForPublishDate" style="">Text 1</label>

My Watir code: 我的Watir代码:

msgText = 'Text 1'
msgText2 = @browser.label(:xpath, '/html/body/div[1]/div[3]/div/div/div/div/div/form/div/div/div[2]/label')
if (msgText == msgText2)
            puts 'Pass' "#{msgText2}"
else 
        puts 'Fail' "#{msgText2}"
end

The problem is that msgText2 (ie @browser.label ) is being set to a Watir::Label element rather than its text. 问题在于msgText2 (即@browser.label )被设置为Watir :: Label元素而不是其文本。

To get the text of the label, you need to call the text method. 要获取标签的文本,您需要调用text方法。 For example: 例如:

msgText2_element = @browser.label(:xpath, '/html/body/div[1]/div[3]/div/div/div/div/div/form/div/div/div[2]/label')
msgText2 = msgText2_element.text

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

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