1

我有這個代碼,我正在使用ruby進行自動化測試。在Selenium Webdriver和Ruby中使用css選擇器

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

我想可以選擇使用「這是新標題」或「標題」爲此,我試圖用find_element CSS選擇器,並使用XPath,但似乎沒有爲我工作搜索的元素。

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

沒有什麼似乎適用於我。我錯過了什麼嗎?

+0

這個問題不同於你以前的問題 - [使用Selenium Webdriver查找元素](http://stackoverflow.com/questions/17932696/find-elements-using-selenium-webdriver)? –

+0

只是試圖更具體的問題。 –

+0

如果你正在澄清這個問題,你應該編輯現有的,而不是創建一個新的。 –

回答

0

是的,你可以做到這一點使用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。你會獲得成功。

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" 
相關問題