简体   繁体   中英

Watir: my browser.button(:class=>'').click is not working

I have the following HTML Structure:

<button class="btn btn-add" data-role="add">Add New User</button>

I am trying to get the class name in my ruby-watir script which is: browser.button(:class => 'btn btn-add').click , but it doesn't work.

How can I click that button using watir?

How many buttons with the same class are there in your page anyway?

  browser.buttons(:class => "btn btn-add").count

usually when_present should take care of the timing issue, add another identifier to help locate the proper element

    browser.button(:class => 'btn btn-add', :text => 'Add New User').when_present.click

Also try this (additional line of code, but could help with the timing issue):

  Watir::Wait.until { browser.button(:class => 'btn btn-add').visible? }<br>
  browser.button(:class => 'btn btn-add', :text => 'Add New User').click

You can also try regex to find your button

browser.button(:text => /Add New User/).when_present.click

Hope this helps

I didn't work with Ruby but you can fix it by adding text attribute (Add New User) to the locator.

like: //button[@class='btn btn-add' and text()='Add New User']

If still doesn't work, as you tried you have to wait for some time using sleep. Though webdriver waits until the entire page is loaded, some elements may take some more time to display/enable.

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