Printing Search Results based on search keyword in Ruby Selenium - ruby

I am very new stackoverflow. So pardon me if I not asked question in right way.
I just started exploring Selenium WebDriver with Ruby.
My problem is as below:
I am navigating to a site(https://www.upwork.com) and entering search keyword(eg:Selenium) in search text box and then I click on Search button. Once I got the Search Results, I am parsing all the search results.
Now I want to print all the names having my search keyword as their skills and I also want to print all names who do not have search keyword as their skills.I tried to do something like this.How can I handle this?
Note: I can use only selenium-webdriver gem
def freelancers_having_skills(skills)
sleep(5)
#allDetails=#driver.find_elements(FREELANCERS) #Capturing all the search results.
#allDetails.each do |item|
temp=item.text
if(temp.include?skills)
temp.each_char {
|c| if (c!='\n')
print c
else
break
end
break
}
puts
end
end
end
Search Results page is like below.
Search Results Page Screenshot

Related

Iterating through the options of a drop down

I'm using Selenium in Ruby ( a language that I am currently learning) and I have a drop down menu that I want to iterate though, select each option, do some stuff, and then move onto the next option.
I have looked at several answers that are somewhat similar. Only one Stack Overflow question had to similar idea in mind as mine but it's in Python and I just don't know the syntax for Ruby.
I have read through the documentation for Ruby and haven't found anything that does anything similar to the Python way.
Essentially what I want to do is:
select first option
click a button
navigate to a different page
download a csv
return back to the previous page
select second option
do the same thing
etc...until all the options are done
Is this possible? I can figure out returning to the previous page and clicking the csv option but I would like some help on the syntax part.
Thank you
The ruby bindings for selenium-webdriver have a Select class for manipulating select lists.
Here's a contrived example that locates a select_list element, passed the element to a Select object, and prints the text of each option in the list. YMMV...
require "selenium-webdriver"
driver = Selenium::WebDriver.for :firefox
driver.navigate.to "https://www.seleniumeasy.com/test/basic-select-dropdown-demo.html"
element = driver.find_element(id: 'select-demo')
select_list = Selenium::WebDriver::Support::Select.new(element)
select_list.options.each { |option| puts option.text}
#=> Please select
#=> Sunday
#=> Monday
#=> Tues
...

RSpec/Ruby verify a value exists in text field

For example purposes, lets use google. Let us also assume in the search bar on google we enter texts that reads, "hello". Lastly lets assume the search bar has a save button and we select it to store the data in the field. So now, when you look at the search bar it reads, "hello".
I want to go back into the browser navigate to google and check that text field to ensure it saved as expected - my data that is. I know how to navigate there. But can someone explain to me in Rspec/Ruby how to go into a browser, find the web element you want to verify the value by way of id, name, xpath, etc....and write a command using selenium webdriver with Rspec/Ruby to do so. NO CAPYBARA.
After entering the search term, you can get the value (e.g. element['value']) from the text field, and use an rspec expectation to validate that the correct string has been entered. For example:
#foo_spec.rb
require 'selenium-webdriver'
require 'rspec'
describe "Contrived Example" do
it "enters a search term" do
driver = Selenium::WebDriver.for :firefox
driver.navigate.to "http://google.com"
element = driver.find_element(:name, 'q')
element.send_keys "test string"
sleep 1
expect(element['value']).to eq "test string"
driver.quit
end
end

#driver.find_element(:id=>"body").text.include?(textcheck) not verifying the text only the id

I am using Selenium-WebDriver for Ruby and I am trying to verify that text is present on a page. I have done many searches and tried many things and the best answer I have found is to use something like
def check_page(textcheck)
if verify {#driver.find_element(:id=>"body").text.include?(textcheck)}
yield it_to "fail"
else
yield it_to "pass"
end
end
The expected outcome if the value of textcheck is present in the body would be pass and if the value of textcheck is not present in the body it would be fail. What is actually happening is if :id=>"body" is present then it is pass and if it is not present then it is fail regardless of .text.include?(textcheck)
If anyone could point me in the right direction for how to verify text is present on a page using Selenium-WebDriver in Ruby it would be greatly appreciated. I have found workarounds for certain cases where I can do
verify {#driver.find_element(:tag_name, 'h1').text!=(textcheck)}
but the element I am trying to verify I can't get to so easily. I looked into css locators and was very confused on how to simplify the tag so I could use it. Any help would be greatly appreciated. Thank you very much. If you require any more information from me please let me know and I will provide it as soon as possible.
I am using Ruby 1.93 with Selenium-WebDriver 2.25 testing in Firefox 14.0.1
I do it this way
#wait = Selenium::WebDriver::Wait.new(:timeout => 30)
begin
#wait.until { #driver.find_element(:tag_name => "body").text.include?("your text")}
rescue
puts "Failure! text is not present on the page"
#Or do one of the options below
#raise
#assert_match "true","false", "The text is not present"
end
UPDATE
Answer to your question in the comments section.
There are two kind of "waits", implicit wait and explicit wait. You can read more about it here. The reason your code failed was because you were searching by "id"=>"body" and not by "tag_name"=>"body". Usually all text is encompassed within the "body" HTML tags in your DOM.

How can I use Nokogiri to find specific text/words on a webpage?

I am new to nokogiri, but it looks like this would be the tool that I would use to scrape a webpage. I am looking for specific words on a webpage. The words are "Valid", "Requirements Met", and "Requirements Not". I am using watir to drive through the website. I currently have:
page = Nokogiri::HTML.parse(browser.html)
to get the html, but I am not sure where to go from here.
Thanks for the help!
If you are using Watir to drive the website, I would suggest using Watir to check for the text. You can get all the text on the page using:
ie.text #Where ie is a Watir::IE
You could then check to see if it has those words are included (by comparing to a regex):
if ie.text =~ /Valid|Requirements Met|Requirements Not/
#Do something if the words are on the page
end
That said, if you are looking for a specific bits of text, you can use Watir to look specifically for those elements (and avoid parsing text or html). If you can provide an HTML sample of what you are working on, we can help find a more robust solution.
I am not sure why you are using both. You could get the page using 'net/http' or mechanize if you just want to check for text. Anyways, you can check for text in watir with browser.text.match 'Valid', same for nokogiri with page.text.match 'Valid'.
You should also be able to use the .text method from Justin's answer along with the standard ruby string .include? method which returns true or false.
if browser.text.include? /Valid|Requirements Met|Requirements Not/
#code to execute if text found
else
#code to execute if text not found
end
This also makes it easy to have a single line validation step if that is what you are after
if using rspec/cucumber
browser.text.should include /Valid|Requirements Met|Requirements Not/
if using test:Unit
assert browser.text.include? /Valid|Requirements Met|Requirements Not/

extract single string from HTML using Ruby/Mechanize (and Nokogiri)

I am extracting data from a forum. My script based on is working fine. Now I need to extract date and time (21 Dec 2009, 20:39) from single post. I cannot get it work. I used FireXPath to determine the xpath.
Sample code:
require 'rubygems'
require 'mechanize'
post_agent = WWW::Mechanize.new
post_page = post_agent.get('http://www.vbulletin.org/forum/showthread.php?t=230708')
puts post_page.parser.xpath('/html/body/div/div/div/div/div/table/tbody/tr/td/div[2]/text()').to_s.strip
puts post_page.parser.at_xpath('/html/body/div/div/div/div/div/table/tbody/tr/td/div[2]/text()').to_s.strip
puts post_page.parser.xpath('//[#id="post1960370"]/tbody/tr[1]/td/div[2]/text()')
all my attempts end with empty string or an error.
I cannot find any documentation on using Nokogiri within Mechanize. The Mechanize documentation says at the bottom of the page:
After you have used Mechanize to navigate to the page that you need to scrape, then scrape it using Nokogiri methods.
But what methods? Where can I read about them with samples and explained syntax? I did not find anything on Nokogiri's site either.
Radek. I'm going to show you how to fish.
When you call Mechanize::Page::parser, it's giving you the Nokogiri document. So your "xpath" and "at_xpath" calls are invoking Nokogiri. The problem is in your xpaths. In general, start out with the most general xpath you can get to work, and then narrow it down. So, for example, instead of this:
puts post_page.parser.xpath('/html/body/div/div/div/div/div/table/tbody/tr/td/div[2]/text()').to_s.strip
start with this:
puts post_page.parser.xpath('//table').to_html
This gets the any tables, anywhere, and then prints them as html. Examine the HTML, to see what tables it brought back. It probably grabbed several when you want only one, so you'll need to tell it how to pick out the one table you want. If, for example, you notice that the table you want has CSS class "userdata", then try this:
puts post_page.parser.xpath("//table[#class='userdata']").to_html
Any time you don't get back an array, you goofed up the xpath, so fix it before proceding. Once you're getting the table you want, then try to get the rows:
puts post_page.parser.xpath("//table[#class='userdata']//tr").to_html
If that worked, then take off the "to_html" and you now have an array of Nokogiri nodes, each one a table row.
And that's how you do it.
I think you have copied this from Firebug, firebug gives you an extra tbody, which might not be there in actual code... so my suggestion is to remove that tbody and try again.
if it still doesn't work ... then follow Wayne Conrad's process that's the best!

Resources