Find elements using css selector/xpath of Selenium Webdriver with Ruby

泄露秘密 提交于 2020-01-15 05:38:08

问题


I have recently started using Selenium to test an app having webviews. Well the code for my webview is:

<div class="class1 class2 class3 class4" style="padding: 4px;" id="_6bd7b91a-214c-4075-b7db-dcaa08155e1a"> 
    <span class="class5" style="font-weight: bold; text-decoration: underline;">This is TITLE</span> 
    <div class="class6" dd:contenttype="content_type1" dd:concept="TITLE" id="_1d8c4490-d0c1-42ed-a93e-e92ca570dd32">
     <div class="class7"> 

I am writing a re-usable code to find the element for automation testing. In the above code, element can be found using 2 ways, I can look for it based on 'dd:concept="TITLE"' or through the text "This is TITLE"; but I could not find anything in Selenium using which I can find the element the way I want to.

I am using "find_element" to look for an element, but there is nothing to which I can pass 'dd:concept="TITLE"' or "This is TITLE" as an arguement.

I tried - driver.find_element(:css, "span.class5") or driver.find_element(:css, "div.class1 class2 class3 class4 span.class5")

Is there anyway I can find the element on the webview using Selenium driver and some text as its arguement??


回答1:


FindElement(By.CssSelector("[dd:concept='TITLE']")) should work.

Ah, you're using ruby.

I did a bit of looking, you could try this:

find_element(:css, "[dd:concept='TITLE']")

Edit once again.

I would suggest escaping the : in the tag name dd:concept, like this:

find_element(:css, "[dd\:concept='TITLE']")



来源:https://stackoverflow.com/questions/17932696/find-elements-using-css-selector-xpath-of-selenium-webdriver-with-ruby

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