how does ruby webdriver get element with hyphen in <name, value> pair

梦想与她 提交于 2019-12-13 03:06:13

问题


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


回答1:


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.




回答2:


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



回答3:


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.



来源:https://stackoverflow.com/questions/17612497/how-does-ruby-webdriver-get-element-with-hyphen-in-name-value-pair

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!