【问题标题】:Using css selector in Selenium Webdriver with Ruby在 Selenium Webdriver 和 Ruby 中使用 css 选择器
【发布时间】:2013-07-29 21:14:02
【问题描述】:

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

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

【问题讨论】:

  • 这个问题和你之前的问题有什么不同 - Find elements using Selenium Webdriver
  • 只是想更具体的问题。
  • 如果您要澄清问题,则应编辑现有问题,而不是创建新问题。

标签: ruby css-selectors selenium-webdriver web-testing ruby-test


【解决方案1】:

是的,您可以使用selenium-webdriver: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。你会成功的。

【讨论】:

    【解决方案2】:

    使用 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-08-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-29
      • 1970-01-01
      • 2020-08-05
      • 1970-01-01
      • 2016-10-29
      • 2012-10-13
      相关资源
      最近更新 更多