I can not locate proper element - click on link Ruby - 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

Related

using watir for a facebook app

recently I'm using watir/ruby to publish on facebook automatically. In simple words I managed to write a sentence in the text field, but I can't click on the button "Publish".
I tried in different ways:
browser.link(:value => '1').click
browser.button(:value => "Publish").click
browser.li(:xpath, '//INPUT[#type="submit"]').click
browser.link(:href =>'javascript:doSubmit()').click
browser.a(:text =>"Publish").click
but I get the error messages: unable to locate element, or element not visible.
I also tried to write:
browser.button(:value => '1').exists?
but I got the answer "false". Can anyone help me?
Thank you very much
The problem is tons of Javascript on the page.
You should wait until your element appears.
Use corresponding functions here
Example:
browser.button(:value => "Publish").wait_until_present.click
Firstly, posting information is not scraping information. I've definitely automated tests for Facebook with a test user account, which was absolutely in line with the TOS.
Is this an element that is showing up in a pop up window? If so you need to do something like: browser.windows.last.use { browser.a(text: 'Publish').click }

Calling an image alt text with Ruby Watir

Ok, I'm trying to access an image stored in an image library which on click closes the image library pop up and inserts the image into the page.
The html for the image is;
<img src="/uploads/images/thumbs/10.png?504" alt="Find Us" width="140" onclick="$('#removeButton', window.opener.document).show();
$('#myImgId', window.opener.document).show();
$('#imageHeadingID', window.opener.document).val('13');
$('#myImgId', window.opener.document).attr('src', '/uploads/images/thumbs/10.png');
window.close()">
As a note, the ?504 is not static and changes each time the image library is open so I cant use it as an id value.
I've tried the following lines of code in turn, all without success (gleaned from google and stack overflow).
find("img[#alt='Find Us']").click
$browser.cell(:text => "Find Us").click
$browser.image(:src => /10/).click
$browser.image(:src => "/10.png").click
$browser.link(:text => "Find Us").click
any help would be appreciated
Try identify by :alt :
$browser.image(:alt => "Find Us").click
But, it's strange, cause, $browser.image(:src => /10/).click - must work also.
I agree with Alex and Justin that using src should work, but
Have you tried using an XPath?
Something like browser.img(:xpath => "/html/.../img").click
You can get the XPath to the image pretty easily with Chrome by right-clicking the image-->Inspect Element-->then right-click on the relevant line of HTML in the developer tools that have opened. If the image is always in the same place on the page (regardless of any change in the src), this method should work.

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

Clicking only on links within a specific menu?

I am trying to scrape a page, and need to click on some links within a menu. If I use the search method, I am then stuck with a Nokogiri object, and therefore can not use the click method.
agent.page.search('.right-menu').links_with(href: /^\/blabla\//).each do |link|
region = link.click
end
The following would tell me that links_with is not defined. How can I make a select links from a specific menu? Is there a way I can parse the object back to a Mechanize object?
You can try something like this:
agent.page.search('.youarehere > a').each do |a|
link = Mechanize::Page::Link.new(a.attr('href'), agent, agent.page)
region = link.click
end
not the cleanest way to do it I guess, but Mechanize is doing almost the same in its source code: http://mechanize.rubyforge.org/Mechanize/Page.html#method-i-links
Would be a nice addition though, instead of going through this.

How to hover over (mouseover) an element in Selenium Ruby?

Anyone know how to hover over an element in Selenium Ruby Webdriver?
My code is like this:
el = driver.find_element(:css => "#foo")
driver.move_to el # How do I trigger a mouseover event on this element?
I'm using selenium-webdriver gem with Firefox in Linux 32-bit.
I used driver.action.move_to(el).perform which differs ever so slightly from the other answers, so I thought I would include it for completeness sake.
Turns out the answer is:
driver.move_to(el).perform
I forgot the .perform.
This works for me:
driver.mouse.move_to el
You need to use Selenium's Action Builder to access more complex actions like hovering (which is what seanny123's answer is demonstrating).
Also, if you are working with a hover, odds are you will need to dynamically wait for it to display before taking your next action (e.g., using an explicit wait).
I put together an example on how to do this -- you can see the full write-up here.
To hover an element:
driver.action.move_to(element).perform
# e.g.
driver.action.move_to(driver.find_element(css: 'a')).perform
To hover an element at a specific location:
driver.action.move_to(element, mouse_x, mouse_y).perform
# e.g.
driver.action.move_to(driver.find_element(css: 'a'), 100, 100).perform

Resources