在 craigslist 网站中找到搜索按钮
Finding the search button in craigslist website
我想制作一些 ruby watir
和 watir-webdriver
代码来搜索 craigslist 的房间。代码需要为搜索按钮找到 HTML 元素。在 html 代码中,它有一个 id=searchbtn。当我将此元素视为 link 或按钮并让我的代码单击它时,我收到一条错误消息,指出找不到该对象。我不确定如何让我的代码找到这个按钮。请帮我。
网址=http://newyork.craigslist.org/search/roo
<button type="submit" id="searchbtn">
<span class="searchicon"></span>
</button>
完整代码在这里-
require 'watir'
require 'rubygems'
# installed watir-webdriver, but did not require it.
craigslist = "http://newyork.craigslist.org/"
section = "search/roo"
website_section = craigslist + section
search_word = "subway"
html_search_text_box_id = "query"
html_button_id = "searchbtn"
search_page_string = "all new york"
# Create browser.
browser = Watir::Browser.new
puts "connecting to craislist at #{website_section}..."
browser.goto website_section
# Maximize browser
# browser.send_keys :f11
# Type there the text in variable
puts "entering search word = #{search_word}..."
browser.text_field(:name, html_search_text_box_id).set search_word
#Waiting for search button to appear
Watir::Wait.until { browser.text.include? search_page_string }
puts "clicking on the search button..."
browser.button(:name, html_button_id).click
if browser.text.include? search_page_string
puts "Test Passed. Found the expected string: #{search_page_string}..."
else
puts "Test Failed! Could not find the expected string: #{search_page_string}..."
end
puts "End of code..."
browser.close
使用 ruby 1.9.3 和 RVM 在 linux ubunut 14 上运行代码。
~projectRoot > ruby craigslist.rb
错误-
/home/john/.rvm/gems/ruby-1.9.3-p551/gems/watir-webdriver-0.6.11/lib/watir-webdriver/elements/element.rb:513:in `assert_exists': unable to locate element, using {:name=>"searchbtn", :tag_name=>"button"} (Watir::Exception::UnknownObjectException)
from /home/john/.rvm/gems/ruby-1.9.3-p551/gems/watir-webdriver-0.6.11/lib/watir-webdriver/elements/element.rb:119:in `click'
from craigslist.rb:29:in `<main>'
我认为您的问题在第 29 行:
browser.button(:name, html_button_id).click
当我查看来源 HTML 时,我看到:
<button type="submit" id="searchbtn"><span class="searchicon"></span></button>
这意味着您希望通过 :id
而不是 :name
来识别该按钮。试试这个,看看搜索是否正确执行:
browser.button(:id, html_button_id).click
我想制作一些 ruby watir
和 watir-webdriver
代码来搜索 craigslist 的房间。代码需要为搜索按钮找到 HTML 元素。在 html 代码中,它有一个 id=searchbtn。当我将此元素视为 link 或按钮并让我的代码单击它时,我收到一条错误消息,指出找不到该对象。我不确定如何让我的代码找到这个按钮。请帮我。
网址=http://newyork.craigslist.org/search/roo
<button type="submit" id="searchbtn">
<span class="searchicon"></span>
</button>
完整代码在这里-
require 'watir'
require 'rubygems'
# installed watir-webdriver, but did not require it.
craigslist = "http://newyork.craigslist.org/"
section = "search/roo"
website_section = craigslist + section
search_word = "subway"
html_search_text_box_id = "query"
html_button_id = "searchbtn"
search_page_string = "all new york"
# Create browser.
browser = Watir::Browser.new
puts "connecting to craislist at #{website_section}..."
browser.goto website_section
# Maximize browser
# browser.send_keys :f11
# Type there the text in variable
puts "entering search word = #{search_word}..."
browser.text_field(:name, html_search_text_box_id).set search_word
#Waiting for search button to appear
Watir::Wait.until { browser.text.include? search_page_string }
puts "clicking on the search button..."
browser.button(:name, html_button_id).click
if browser.text.include? search_page_string
puts "Test Passed. Found the expected string: #{search_page_string}..."
else
puts "Test Failed! Could not find the expected string: #{search_page_string}..."
end
puts "End of code..."
browser.close
使用 ruby 1.9.3 和 RVM 在 linux ubunut 14 上运行代码。
~projectRoot > ruby craigslist.rb
错误-
/home/john/.rvm/gems/ruby-1.9.3-p551/gems/watir-webdriver-0.6.11/lib/watir-webdriver/elements/element.rb:513:in `assert_exists': unable to locate element, using {:name=>"searchbtn", :tag_name=>"button"} (Watir::Exception::UnknownObjectException)
from /home/john/.rvm/gems/ruby-1.9.3-p551/gems/watir-webdriver-0.6.11/lib/watir-webdriver/elements/element.rb:119:in `click'
from craigslist.rb:29:in `<main>'
我认为您的问题在第 29 行:
browser.button(:name, html_button_id).click
当我查看来源 HTML 时,我看到:
<button type="submit" id="searchbtn"><span class="searchicon"></span></button>
这意味着您希望通过 :id
而不是 :name
来识别该按钮。试试这个,看看搜索是否正确执行:
browser.button(:id, html_button_id).click