I have this code for which I am doing automation testing using ruby.

<div class="class1 class2 class3 class4" style="padding: 4px;" id="_d4b99a9f-d1c8-4587-94e2-5ee95ebf1f76"> 
    <span class="class5" style="font-weight: bold; text-decoration: underline;">This is new TITLE</span> 
    <div class="class6" dd:contenttype="TEST CONTENT TYPE" dd:concept="TITLE" id="_7cfd6eed-fa15-42b8-81af-d09bf3cd4460">
     <div class="class7"> 
      <div class="class8" dd:contenttype="TEST CONTENT" dd:entityid="0" dd:entityversion="0" id="_1c9125c4-b078-4017-a5ad-c48210e7090b"> 
       <div class="class9 class10" dd:btnfloatingstyle="top-right" dd:entitytexttype="resultval" id="_9c41159a-3a5b-4de1-87d2-e3361bd4d746" contenteditable="true"></div> 
      </div> 
     </div>
    </div> 
   </div> 

I want to search the element using either 'This is new TITLE' or 'TITLE' for which I tried find_element with css selector and also with xpath but none seemed to work for me.

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

Nothing seemed to work for me. Am I missing something?

有帮助吗?

解决方案

Yes,you can do that using selenium-webdriver and :xpath. Take a look at the below example:

require 'selenium-webdriver'

driver = Selenium::WebDriver.for :firefox
driver.navigate.to "http://en.wikipedia.org/wiki/Ruby_(programming_language)"

driver.find_element(:xpath,"//span[@class = 'mw-headline' and text() = 'Ruby 1.0' ]").tag_name
# => "span"
driver.find_element(:xpath,"//span[@class = 'mw-headline' and text() = 'Ruby 1.0' ]").text
# => "Ruby 1.0"

This example is fully equivalent to your html code. Now try to create your xpath,by taking the help of the above. You will get success.

其他提示

Use nokogiri gem

require 'nokogiri'

$page_html = Nokogiri::HTML.parse(browser.html)

$page_html.css("span.ddsectiondisplay").text
=>"This is new Title"

Or if you're just using watir.

browser.title
=>"title of page" 
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top