2

我有这段代码,我正在使用 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> 

我想使用 'This is new TITLE' 或 'TITLE' 搜索元素,为此我尝试了 find_element 与 css 选择器以及 xpath,但似乎没有一个对我有用。

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

似乎没有什么对我有用。我错过了什么吗?

4

2 回答 2

0

使用 nokogiri 宝石

require 'nokogiri'

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

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

或者,如果您只是使用 watir。

browser.title
=>"title of page" 
于 2013-07-29T21:57:03.303 回答
0

是的,您可以使用selenium-webdriverand来做到这一点:xpath。看看下面的例子:

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"

html此示例与您的代码完全等效。现在尝试xpath通过上述帮助创建您的 . 你会获得成功。

于 2013-07-30T06:38:53.533 回答