2013-12-16 33 views
-2

錯誤當試圖執行,這是一個屏幕刮板我一直在使用水豚和硒web驅動程序工作。這是確切的錯誤:給定的選擇器#無效或不會導致WebElement。發生以下錯誤:(Selenium :: WebDriver :: Error :: InvalidSelectorError)。這與選擇器應該找到下一頁鏈接並點擊它有關。檢查來源時,它是一個有效的xpath選擇器,但水豚和硒不同意。Selenium :: WebDriver :: Error :: InvalidSelectorError當找到XPath選擇器的下一頁鏈接

require "capybara/dsl" 
require "spreadsheet" 
require "fileutils" 
require "open-uri" 


Capybara.run_server = false 
Capybara.default_driver = :selenium 
Capybara.default_selector = :xpath 
Spreadsheet.client_encoding = 'UTF-8' 

class Tomtop 
    include Capybara::DSL 

    def initialize 
    @LOCAL_DIR = "data-hold/images" 
    @excel = Spreadsheet::Workbook.new 
    @work_list = @excel.create_worksheet 
    @row = 0 
    FileUtils.makedirs(@LOCAL_DIR) unless File.exists? @LOCAL_DIR 
    end 

    def go 
    visit_main_link 
    end 

    def retryable(options = {}, &block) 
    opts = { :tries => 1, :on => Exception }.merge(options) #possible bug (remove this line and take options from options hash) 

    retry_exception, retries = opts[:on], opts[:tries] 

    begin 
     return yield 
    rescue retry_exception 
     retry if (retries -= 1) > 0 
    end 

    yield 
    end 

    def visit_main_link 
    visit "http://www.example.com/clothing-accessories?dir=asc&limit=72&order=position" 
    @results = all("//h5/a[contains(@onclick, 'analyticsLog')]") 
    while page.has_selector?("//td[contains(@class, 'pages')]//a[img/@alt='Next Page']") 
     retryable(:tries => 1, :on => OpenURI::HTTPError) do 
     find.first("//td[contains(@class, 'pages')]//a[img/@alt='Next Page']").click 
     @results = all("//h5/a[contains(@onclick, 'analyticsLog')]") 
     @results.each do |a| 
     @links << a[:href] 
     end 
      @links.each do |link| 
      visit link 
      save_item 
      end 
     @excel.write "inventory.csv" 
     end 
    end 
    end 

    def save_item 
    data = all("//*[@id='content-wrapper']/div[2]/div/div") 
    data.each do |info| 
     @work_list[@row, 0] = info.find("//*[@id='productright']/div/div[1]/h1").text 

     price = info.first("//div[contains(@class, 'price font left')]") 
     @work_list[@row, 1] = (price.text.to_f * 1.33).round(2) if price 

     @work_list[@row, 2] = info.find("//*[@id='productright']/div/div[11]").text 

     @work_list[@row, 3] = info.find("//*[@id='tabcontent1']/div/div").text.strip 

     color = info.all("//dd[1]//select[contains(@name, 'options')]//*[@price='0']") 
     @work_list[@row, 4] = color.collect(&:text).join(', ') 

     size = info.all("//dd[2]//select[contains(@name, 'options')]//*[@price='0']") 
     @work_list[@row, 5] = size.collect(&:text).join(', ') 

     sku = File.basename(info.find("//*[@id='content-wrapper']/div[2]/div/div/div[1]/div[1]/a")['href']) 
     @work_list[@row, 6] = sku.gsub!(/\D/, "")#.join(([*('A'..'Z'),*('0'..'9')]-%w(0 1 I O)).sample(4).join) 

     @work_list[@row, 7] = File.basename(info.find("//*[@id='content-wrapper']/div[2]/div/div/div[1]/div[1]/a")['href']) 

     imagelink = info.all("//*[@rel='lightbox[rotation]']") 
     @work_list[@row, 8] = imagelink.map { |link| File.basename(link['href']) }.join(', ') 

     images = imagelink.map { |link| link['href'] } 
     images.each do |image| 
     File.open(File.basename("#{@LOCAL_DIR}/#{image}"), 'w') do |f| 
      f.write(open(image).read) 
     end 
     end 
     @row = @row + 1 
    end 
    end 
end 

tomtop = Tomtop.new 
tomtop.go 
+1

這是什麼故障? – Arran

+0

第46行似乎是問題,但上面幾行使用相同的xpath選擇器評估罰款:page.has_selector?(「// td [contains(@class,'pages')] // a [img/@ alt = 'Next Page']「) – jcuwaz

+0

你跑什麼瀏覽器? – Arran

回答

0

我覺得你的問題是在這裏:

find.first("//td[contains(@class, 'pages')]//a[img/@alt='Next Page']").click 

更具體地說:

a[img/@alt='Next Page'] 

我以前從未見過這種符號在XPath的,所以這是我的猜測,這是無效的。對我來說,這意味着:

Find <a img='Next Page' alt='Next Page' />

但既然你@匹配的屬性我敢肯定,這是不正確的符號。

修復您的選擇器以匹配您需要的選擇。例如,如果你正在努力尋找那就是下<a />圖像,那麼你應該使用

a/img[@alt='Next Page'] 

另一項建議是我應該做,是使用CSS選擇器。他們是faster, and more readable.

+0

我不明白爲什麼它是無效的,當它可以在頁面上找到問題。我會用css選擇器來試試它;你能否在下一頁提供下一頁鏈接的建議:http://www.tomtop.com/clothing-accessories?dir=asc&limit=72&order=position – jcuwaz

+0

當然可以!你的選擇器是:'td.pages li:nth-​​child(6)a [href * ='&p']'但請記住這個選擇器找到_both_分頁部分。我不確定您的測試是否需要區分這些 – sircapsalot

+0

我正在使用find.first來處理它,因此您的應該是:find.first(:css,「td.pages li:nth-​​child(6)a [href * ='&p']「)。點擊但仍會拋出相同的錯誤。 (使用xpath作爲默認的選擇器,因此切換回css) – jcuwaz

相關問題