Unable to click on link using Link text/Partial Link text using ruby - ruby

I am new here and even to Ruby and Selenium. I am trying to click a link on web page which has following code:
<a> target="mainFrame" href="dynamic_Utility_Index.htm">Dynamic Utilities</a>
So basically I want to click on this dynamic utilities. The script which I have written so far is:
require 'selenium-webdriver'
require 'win32ole'
driver = Selenium::WebDriver.for:firefox
driver.manage().window().maximize();
driver.navigate.to 'xyz.com'
wait = Selenium::WebDriver::Wait.new(:timeout =>10) # seconds
#Click on Dynamic Utilities
wait.until{driver.find_element(:link_text,'dynamic_Utility_Index.htm').click}
puts "Clicked"
I have even used link, partial_link_text in place of link_text but keep getting the following error
(Unable to locate element: {"method":"link
text","selector":"dynamic"})
(Selenium::WebDriver::Error::TimeOutError)
I am using Ruby and Selenium Web driver.

Did you try with:
wait.until{driver.find_element(:link_text,'DYNAMIC UTLITIES').click}
?? Sometimes I had to use the text link by capitalized.
Another option could be:
driver.find_element(:xpath, "//a[contains(#target, 'mainFrame')]").click
Hope works for you :D

The item you consider to be a link text (dynamic_Utility_Index.htm) actually just a value of href attribute. Actual link text is "Dynamic Utilities". Also you can use xpath locator //a[#href="dynamic_Utility_Index.htm"] instead of search by link text:
wait.until{driver.find_element(:xpath,'//a[#href="dynamic_Utility_Index.htm"]').click}

Related

Finding element and entering in Ruby Selenium

I'm new to the Selenium and Ruby and Cucumber/Gherkins world and am trying a simple script to navigate to the Google page, find the search bar and enter a word and press enter or find the "Google Search" element and click.
This is in a Ruby file but formatted in Gherkins as I'm working with it.
require 'rubygems'
require 'selenium-webdriver'
driver = Selenium::WebDriver.for :chrome
Given(/^I am on the Google website$/) do
driver.navigate.to "http://www.google.com"
end
When(/^search is entered$/) do
search = driver.find_element(xpath: "//div[#class = 'jhp big']//input[#class = 'gLFyf gsfi']")
search.send_keys "this"
end
Then(/^confirm$/) do
puts "Confirmed"
driver.close
end
So here I'm navigating to the google website using a Selenium WebDriver initialized as driver. Then finding the element using the xpath and sending in the word 'this'.
When I run this, I get this error:
Selenium::WebDriver::Error::NoSuchElementError: no such element: Unable to locate element: {"method":"xpath","selector":"//div[#class = 'jhp big']//input[#class = 'gLFyf gsfi']"}
Also, to click 'enter' I can either do send_keys :enter or find the Search button and use 'click' correct?
Thank you in advance
you have to use implicit wait for driver to wait until element found
Write the following code and then fit into your cucumber model
require 'selenium-webdriver'
driver=Selenium::WebDriver.for :chrome
driver.navigate.to("https://www.google.com/")
driver.manage.timeouts.implicit_wait=10
driver.find_element(name: 'q').send_keys 'raja'
driver.find_element(name: 'btnK').click
But remember, If you WATIR which is a wrapper around selenium binding these kind of waiting is automatic.

I can not locate proper element - click on link Ruby

I tried to click on link (see screenshots)
http://imgur.com/q66g7z6
http://imgur.com/KNF1y7z
I tried using few examples
e.g
#browser.button(:class=> '//*[#class="login"]//ul/li[0]/a').click
and
browser.button(:xpath=> "//a[#data-viewmodel='PagesAsync/RegisterPrivate/RegisterPrivateViewModel']").click
but is not correct
I can see the message that unable to locate element
Can somebody help?
The main problem is that you are telling Watir to look for a button when you actually want a link. While the UI may be styled to look like a button, you will notice that the HTML has a a tag instead.
The first example, which also has the wrong locator type, should be:
#browser.link(:xpath => '//*[#class="login"]//ul/li[0]/a').click
The second example should be:
browser.link(:xpath => "//a[#data-viewmodel='PagesAsync/RegisterPrivate/RegisterPrivateViewModel']").click
Note that the second example would be more Watir-like if you use the normal attribute locators:
browser.link(data_viewmodel: 'PagesAsync/RegisterPrivate/RegisterPrivateViewModel').click
There are two options. One is get your developers to add better IDs.
If that is not possible, try this:
how does ruby webdriver get element with hyphen in <name, value> pair
It worked for me in several similar situations.
I wonder how you can find a button by using an xpath to a link. It is also not clear whether you use browser or #browser. You would need to look into how the browser instance is defined, which likely is one of these:
#browser = Watir::Browser.new :chrome
###or###
browser = Watir::Browser.start 'example.com', :firefox
and if you haven't create a browser instance, then you would need to do it before you can use Watir-Webdriver. ;)
As for your question, you could try searching using the text if it is unique like this, though it may be a brittle test:
#browser.div(:class => 'login').link(:text => /For priva/).click
but I would recommend to double check the number of elements found using the div and link locators like this to make sure you got the right element:
#browser.divs(:class => 'login').length
#browser.div(:class => 'login').links(:text => /For priva/).length

Ruby Mechanize: Programmatically Clicking a Link Without Knowing the Name of the Link

I am writing a ruby script to search the web. Here is the code:
require 'mechanize'
mechanize = Mechanize.new
page = mechanize.get('http://www.example.com/)
example_page = page.link_with(:text => 'example').click
puts example_page.body
The code above works alright. The text 'example' ((:text => 'example') has to be a link on the page for the code to work correctly. The problem, however, is that when I do a web search (bing, yahoo, google, etc), hundreds of links show up. How can I programmatically click a link without knowing the exact name of the link? I want to be able to click a link if the name of the link partly (or fully) matches a text that I specify or click a link if it has a certain url. Any help would be appreciated.
Mechanize has regular expressions:
page.link_with(text: /foo/).click
page.link_with(href: /foo/).click
Here are the Mechanize criteria that generally work for links and forms:
name: name_matcher
id: id_matcher
class: class_matcher
search: search_expression
xpath: xpath_expression
css: css_expression
action: action_matcher
...
If you're curious, here's the Mechanize ElementMatcher code

how do I loop a click action(refresh) until a specific string is found?

I am writing a watir script where I need to loop a click action untill a text is found. Please let me know how to do this.
thanks
You could do something like this:
require 'watir'
b = Watir::Browser.new
b.goto('http://www.example.com')
until b.text.include? "example"
b.refresh
end
But watir and watir-webdriver have methods to handle dynamic page elements. This link has examples on waiting with watir-webdriver.

stumped on clicking a link with nokogiri and mechanize

perhaps im doing it wrong, or there's another more efficient way. Here is my problem:
I first, using nokogiri open an html document and use its css to traverse the document until i find the link which i need to click.
Now once i have the link, how do i use mechanize to click it? According to the documentation, the object returned by Mechanize.new either the string or a Mechanize::Page::Link object.
I cannot use string - since there could be 100's of the same link - i only want mechanize to click the link that was traversed by nokogiri.
Any idea?
After you have found the link node you need, you can create the Mechanize::Page::Link object manually, and click it afterwards:
agent = Mechanize.new
page = agent.get "http://google.com"
node = page.search ".//p[#class='posted']"
Mechanize::Page::Link.new(node, agent, page).click
Easier way than #binarycode option:
agent = Mechanize.new
page = agent.get "http://google.com"
page.link_with(:class => 'posted').click
That is simple, you don't need to use mechanize link_with().click
You can just getthe link and update your page variable
Mechanize saves current working site internally, so it is smart enough to follow local links
Ex.:
agent = Mechanize.new
page = agent.get "http://somesite.com"
next_page_link = page.search('your exotic selectors here').first rescue nil #nokogyri object
next_page_href = next_page_link['href'] rescue nil # '/local/link/file.html'
page = agent.get(next_page_href) if next_page_href # goes to 'http://somesite.com/local/link/file.html'

Resources