从没有不同 URL 的网站中删除结果

发布于 2024-12-18 05:21:01 字数 1605 浏览 2 评论 0原文

我正在尝试使用 SayNoTo0870 自动化搜索替代电话号码的过程。每次搜索备用号码或名称时,都会显示 '/companysearch.php' 页面。

显然这个页面没有任何参考,在我看来你不能仅仅链接到这个页面。

我希望做的是使用下面的代码,自动打开浏览器、搜索名称/号码、删除 HTML,然后提供前 5 个结果。我已经完成了自动化部分,但显然,当尝试使用 Hpricot 保存网页时,它只会显示“抱歉,找不到任何页面”,因为我无法直接链接到搜索结果页面。

到目前为止,这是我的代码: (我删除了注释以缩短它)

require 'rubygems'
require 'watir'
require 'hpricot'
require 'open-uri'



class OH870


    def searchName(name)
        browser = Watir::Browser.new
        browser.goto 'http://www.saynoto0870.com/search.php'
        browser.text_field(:name => 'search_name').set name
        browser.button(:name => 'submit').click
    end 

    def searchNumber(number)

        browser = Watir::Browser.new
        browser.goto 'http://www.saynoto0870.com/search.php'
        browser.text_field(:name => 'number').set number
        browser.button(:name => 'submit').click
    end 

    def loadNew(website)

        doc = Hpricot(open(website))
        puts(doc)   

    end


    def strip_tags
        stripped = website.gsub( %r{</?[^>]+?>}, '' )
        puts stripped
    end

end # class

class Main < OH870
puts "What is the name of the place you want?" 
website = 'http://www.saynoto0870.com/companysearch.php'

    question = gets.chomp
    whichNumber = OH870.new
    whichNumber.searchName(question)
    #result = OH870.new
    #withoutTags = website.strip_tags
    #result.loadNew(withoutTags)
end

现在我不确定是否有一种方法“要求 watir 跟踪companysearch.php页面并转储结果,而不必将此页面作为变量传递。

我想知道是否有人有什么建议吗?

I'm trying to automate the process of searching for alternative telephone numbers using SayNoTo0870 . Every time one searches for an alternate number or name it brings up the '/companysearch.php' page.

Clearly this page has no reference, and in my mind you can't just link to this page.

What I'm hoping to do is use the code below, to automate the opening of a browser, searching of a name/number, stripping out the HTML and then providing the top 5 results. I've got the automation part down, but clearly when trying to save the webpage using Hpricot it only brings up the 'Sorry nothing can be found page' because I can't link directly to the search result page.

Here is my code thus far:
(I've removed comments to shorten it)

require 'rubygems'
require 'watir'
require 'hpricot'
require 'open-uri'



class OH870


    def searchName(name)
        browser = Watir::Browser.new
        browser.goto 'http://www.saynoto0870.com/search.php'
        browser.text_field(:name => 'search_name').set name
        browser.button(:name => 'submit').click
    end 

    def searchNumber(number)

        browser = Watir::Browser.new
        browser.goto 'http://www.saynoto0870.com/search.php'
        browser.text_field(:name => 'number').set number
        browser.button(:name => 'submit').click
    end 

    def loadNew(website)

        doc = Hpricot(open(website))
        puts(doc)   

    end


    def strip_tags
        stripped = website.gsub( %r{</?[^>]+?>}, '' )
        puts stripped
    end

end # class

class Main < OH870
puts "What is the name of the place you want?" 
website = 'http://www.saynoto0870.com/companysearch.php'

    question = gets.chomp
    whichNumber = OH870.new
    whichNumber.searchName(question)
    #result = OH870.new
    #withoutTags = website.strip_tags
    #result.loadNew(withoutTags)
end

Now I'm not sure whether there's a way of "asking watir to follow through to the companysearch.php page and dump the results without having to pass this page as a variable.

I wonder if anyone has any suggestions here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

素年丶 2024-12-25 05:21:01

使用 WATIR,减去无关的库,这就是完成您所描述的内容所需的全部内容(仅使用“名称”测试用例)。我已经将其从函数格式中提取出来,因为您已经知道如何做到这一点,这将是一个更清晰的测试用例路径。

require 'watir'

@browser = Watir::Browser.new :firefox   #open a browser called @browser

@browser.goto "http://(your search page here)"         #go to the search page
@browser.text_field(:name => 'name').value = "Awesome" #fill in the 'name' field
@browser.button(:name => 'submit').click               #submit the form

如果一切顺利,我们现在应该查看搜索结果。 WATIR 已经知道它位于新页面上 - 我们不必指定 URL。如果结果位于框架中,我们确实需要先访问该框架,然后才能查看其内容。让我们假设它们位于 ID 为“search_results”的 DIV 元素中:

results = @browser.div(:id => "search_results").text

resultsFrame = @browser.frame(:index => 1)                #in the case of a frame
results = resultsFrame.div(id => "search_results).text

如您所见,您不需要保存整个页面来解析结果。它们可以位于表格单元格中,也可以位于每行的不同 div 中,或者位于新框架中。所有这些都可以使用 WATIR 轻松访问,存储在变量、数组中,或立即写入控制台或日志文件。

@results = Array.new                #create an Array to store our results

@browser.divs.each do |div|         #for each div element on the page
   if div.id == "search_results"    #if the div ID equals "search_results"
      @results << div.text          #add it to our array named @results
   end
end

现在,如果您只想要前 5 个有很多方法可以访问它们< /a>.

@results[0]      #first element
@results[0..4]   #first 5 elements

我还建议您研究一些编程原则,例如 DRY(不要重复自己)。在您的函数定义中,您会看到它们共享代码,例如打开浏览器并访问相同的 URL - 您可以合并这些:

def search(how, what)
  @browser = Watir::Browser.new :firefox
  @browser.goto "(that search url again)"
  @browser.text_field(:name => how).value = what
  etc...
end

search("name", "Hilton")
search("number", "555555")

因为我们知道两个可用的 text_field 名称是“name”和“number”,并且这些名称具有良好的逻辑性作为一种“方式”,我们可以将它们参数化,并为“按名称搜索”和“按数字搜索”测试用例使用单个函数。只要测试用例保持足够相似以进行共享,这就会更有效。

With WATIR, minus the extraneous libraries, here's all it takes to accomplish what you've described (using the 'name' test case only). I've pulled it out of the function format since you already know how to do that, and this will be a clearer test case path.

require 'watir'

@browser = Watir::Browser.new :firefox   #open a browser called @browser

@browser.goto "http://(your search page here)"         #go to the search page
@browser.text_field(:name => 'name').value = "Awesome" #fill in the 'name' field
@browser.button(:name => 'submit').click               #submit the form

If all goes well, we should now be looking at the search results. WATIR already knows it's on a new page - we don't have to specify a URL. In the case that the results are in a frame, we do need to access that frame before we can view its content. Let's pretend they're in a DIV element with an ID of "search_results":

results = @browser.div(:id => "search_results").text

resultsFrame = @browser.frame(:index => 1)                #in the case of a frame
results = resultsFrame.div(id => "search_results).text

As you can see, you do not need to save the entire page to parse the results. They could be in table cells, they could be in a different div per line, or a new frame. All are easily accessible with WATIR to be stored in a variable, array, or immediately written to the console or log file.

@results = Array.new                #create an Array to store our results

@browser.divs.each do |div|         #for each div element on the page
   if div.id == "search_results"    #if the div ID equals "search_results"
      @results << div.text          #add it to our array named @results
   end
end

Now, if you just wanted the top 5 there are many ways to access them.

@results[0]      #first element
@results[0..4]   #first 5 elements

I'd also suggest you look into a few programming principles like DRY (Don't Repeat Yourself). In your function definitions where you see that they share code, like opening the browser and visiting the same URL - you can consolidate those:

def search(how, what)
  @browser = Watir::Browser.new :firefox
  @browser.goto "(that search url again)"
  @browser.text_field(:name => how).value = what
  etc...
end

search("name", "Hilton")
search("number", "555555")

Since we know that the two available text_field names are "name" and "number", and those make good logical sense as a 'how', we can parameterize them and use a single function for both the Search by Name and Search by Number test cases. This is more efficient, as long as the test cases remain similar enough to be shared.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文