结合Ruby在SeleniumWebDriver中使用css选择器

结合Ruby在SeleniumWebDriver中使用css选择器,ruby,css-selectors,selenium-webdriver,web-testing,ruby-test,Ruby,Css Selectors,Selenium Webdriver,Web Testing,Ruby Test,我有这段代码,我正在使用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

我有这段代码,我正在使用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> 
对我来说似乎什么都不管用。我遗漏了什么吗?

使用nokogiri宝石

require 'nokogiri'

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

$page_html.css("span.ddsectiondisplay").text
=>"This is new Title"
或者你只是在用瓦蒂尔

browser.title
=>"title of page" 
是的,您可以使用和来完成。请看下面的示例:

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
。你会成功的。

这个问题与你以前的问题不同吗?只是想让问题更具体一些。如果你要澄清问题,你应该编辑现有的问题,而不是创建新的问题。
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"