Domanda

I have html like below and I want to get the element by "sku-code"(there is a hyphen in it)

<div class="leavemessagebox" style="position: relative;" sku-code="m_showcase">

when I used

browser.div(:sku-code=>'m_showcase')

ruby reported an error

ERROR:undefined local variable or method `code' for #<AUT::WebClient:0x2c59650>

It sames ruby can't recognize "sku-code" as a name, anyone can give me any suggestion about how to get the element by "sku-code"? thanks. Sorry for not explain myself clearly. there are many elements that have sku-code selector and I want to collect them all in a list, so the class name and tag name isn't stable. how can i do that

È stato utile?

Soluzione

Looks like Watir WebDriver. Try use css (preferred) or xpath.

browser.element(:css => "[sku-code='m_showcase']") # single one
browser.elements(:css => "[sku-code='m_showcase']") # a list of all matches

Documention on CSS's attribute selector is here.

So basically the above selector finds all elements with attribute sku-code equals to m_showcase.

Altri suggerimenti

:sku-code is not a valid symbol literal. It is however valid Ruby code, it is parsed as:

:sku - code

So, you are trying to call a method named code and subtracting its return value from the symbol :sku, which obviously doesn't make sense.

But you can quote the symbol literal, of course:

:'sku-code'

browser.element(:css => ".leavemessagebox[sku-code='m_showcase']") I think this will still the specific CSS.

Please Let me know is the above CSS is working or Not.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top