Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>Short answer:</strong> Adding class names to source code is the correct direction and should be considered as good practice, however, after that, using <code>:class</code> locator isn't good enough in most of the case, try use <code>:xpath</code> or <code>:css</code>. Therefore, you, as a developer, go ahead and add the class names, but you need make sure your QA people know how to use Watir, don't simply use <code>:id</code> or <code>:class</code> for all locators.</p> <p><strong>Long answer:</strong> If the site is simple enough, adding IDs would be easiest and the best one. However, nowadays, many JavaScript frameworks like <a href="http://docs.sencha.com/extjs/4.2.1/extjs-build/examples/build/KitchenSink/ext-theme-neptune/" rel="nofollow">ExtJS</a>, create dynamic IDs, so in that case adding class names to source code would be better.</p> <p>After adding the class names, for example in your case, using <code>:class</code> locator is a bad choice, which might be even worse than <code>:id</code>, as IDs are supposed to be unique. For complex pages, <code>:class</code> locator is pretty much useless, which will find unwanted elements.</p> <p>Here, your error message means you might have more than one elements with class <code>save-btn</code>, the first one isn't visible to be interacted with. </p> <p>Selenium WebDriver or Watir WebDriver, both support XPath and CssSelector, so you should use <code>:xpath</code> or <code>:css</code> when necessary, instead of <code>:id</code>, <code>:class</code> etc.</p> <p>For example, something like:</p> <pre><code>links(:item, :css=&gt; '.save-btn:not([style*='display:none'])')[0] </code></pre> <p><strong>Jarmo Pertman</strong> suggests using ID/Class is in favor of XPath/Css Selectors, which is not completely correct. ID/Class are just subsets of XPath/Css Selectors, if it's easy enough, use ID/Class, however unnecessary chaining is not a good practice, in the example he gave,</p> <pre><code>browser.div(id: "container").span(class: "save-btn") </code></pre> <p>is equivalent to CSS selector <code>div#container span.save-btn</code>, therefore, there are no fragile or maintenance issues for CSS selector, because they are identical.</p> <p>XPath/CSS selectors are powerful tools, everyone should be learning, but only use them if really needed. Bad XPath/Css Selector are fragile, try find the good ones. (But bear in mind that XPath is slow, should be considered as last option)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload