2015-06-26 81 views
0

我需要的Ruby /硒红宝石/硒值到数组

一些帮助,我有页面上的一些内容是这样的:

<a class="Name" title="Migel" href="/profiles/users/_1324567980000000c/">Migel</a> 

我想给HREF字符串值到数组。

当我使用 “find_element” - ALL OK:

profile = driver.find_element(:xpath, "****").attribute ('href') 

但页面包含了很多这样的元素,我尝试使用

profile_array = driver.find_elements(:xpath, "lbuh-bluh").attribute ('href') 

自然是attribute ('href')不工作,阵列。 在这种情况下我该怎么办?

一般

,我在执行“随机点击”页面上的任务,我想收集所有的“href”的元素,我需要到数组,然后实现了这样的事情:

array[srtring1, string2....stringN].sample.click(示意图)

回答

0

只会映射到元素并获得href属性是否足够?

profile_array = driver 
    .find_elements(:xpath, "lbuh-bluh") 
    .map { |el| el.attribute('href') } 
+0

非常感谢。我早些时候使用.map与“find_element”哈哈哈!谢谢 – Dmytro

0

我看,这是某种使用Ruby解析HTML的,如果这是你为什么不使用Nokogiri宝石的情况下,可以说你有类似:

<div id="funstuff"> 
     <p>Here are some entertaining links:</p> 
     <ul> 
     <li><a href="http://youtube.com">YouTube</a></li> 
     <li><a data-category="news" href="http://reddit.com">Reddit</a></li> 
     <li><a href="http://kathack.com/">Kathack</a></li> 
     <li><a data-category="news" href="http://www.nytimes.com">New York Times</a></li> 
     </ul> 
    </div> 

是哪个一个名为的内容可以说:selenium.html位于桌面:C:\Desktop\selenium.html

这里是Ruby代码HANDELS是:

page = Nokogiri::HTML(open("C:\Desktop\selenium.html")) 
    links = page.css("a") 
    profile_array = [] 
    links.each{|ref| profile_array << ref["href"] } 

    puts profile_array 

=>http://youtube.com 
=>http://reddit.com 
=>http://kathack.com/ 
=>http://www.nytimes.com