简体   繁体   中英

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. But I get this instead:

  Fail #<Watir::Browser:0x00000004440b98>

It looks like my variable isnt set to text i want to set.

my 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:

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.

To get the text of the label, you need to call the text method. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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