简体   繁体   中英

Does watir-webdriver have a “wildcard tag”?

You know how you can search a page's source like this:

browser.div(:text => /foo/)

How can you do something like this:

browser.wildcard(:text => /foo/)

I'm looking for this functionality because I visit page 1 which has

<tag1>foo</tag1>

but page 2 has

<tag2>foo</tag2>

and it is a certainty that foo occurs nowhere else in the page for both pages.

Element will find any element that matches regardless of tag name:

browser.element(text: /foo/)

Justin makes a great point in the comments. Pairing #element with a regex value will result in returning the html element.

If you want the last tag that references it (note that this is probably a prohibitively non-performant option):

browser.element(text: /foo/, index: -1)

If there aren't any issues with nested spans, this would be a slightly faster option:

b.element(text: /foo/, tag_name: /span|td/)

In general, it is much much better to avoid using regular expression matchers when you don't *have to use them.

How about xpath?

browser.element(xpath: "//tag1[contains(text(),'foo')]")

If you have only one tag1 on the page you can even:

browser.element(xpath: "//tag1")

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