问题
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