Calling an image alt text with Ruby Watir - ruby

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.

Related

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

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 }

Access button in div popup watir web-driver

I am trying to retrieve, modify, and reinsert some text in a pop up dialog using watir web-driver. All was going swimmingly until I tried to find a way to click the "Save Book Info" button pictured here: http://i.stack.imgur.com/pGdln.png
However I am unable to find the button. To access the pop up box I gather all the links with the correct name into an array:
#browser.imgs.each do |img| #The links are imgs
if img.title.include?('Edit/Delete Book')
#books << img
end
end
From there I can access the links with #books[index].click. I am able to access and edit the text_fields with:
url = #browser.text_field(:name=>'url').value
... do stuff with url ..
#browser.text_field(:name=>'url').set url
But when I try and find the "Close" button I only get the buttons that are on the main page. After many headaches I managed to find the title of the window I want to work with using #browser.div(:class=>'dijitDialogTitleBar').title, which returns "Edit Book". Success! No. I still can't figure out how to access the buttons on that popup!
If anyone could help me with this I would be most grateful. This is my first time using Watir so it's probably a simple answer...
If more information is needed I'd be happy to supply it. Thanks in advance.
EDIT
Here is the html for the button:
<span id="dijit_form_Button_2_label"
class="dijitReset dijitInline dijitButtonText"
dojoattachpoint="containerNode">Save Book Info</span>
It looks like you have a span tag that is styled to be looked like a button. You need to access it is a span instead of a button.
Try:
#browser.span(:text => 'Save Book Info').click

Clicking a button with WATIR WebDriver

I have been struggling with this all morning and was hoping to get some assistance.
I have this button on a webpage that pulls up a control panel that lets the user select which widgets to display. I'm trying to get this to fire using watir webdriver and firefox 9.0 but having no luck at all. I'm able to login to the site, get to the page that contains the button but just can't click the 'CHOOSE YOUR FEATURES' button.
<div class="personalizationButton">
<span class="personalizationStatus"></span>
<b class="widgetPanelLink neo-button button-white">
<span>CHOOSE YOUR FEATURES</span>
</b>
</div>`
I've been concentrating on using div class 'personalizationButton' but perhaps I need to point to that 'b class'? Just not sure how to format it. Here are some examples of what I have tried
$browser.div(:class, 'personalizationButton').when_present.click
$browser.button(:text => 'CHOOSE YOUR FEATURES').click
$browser.button(:div => 'personalizationButton').click
etc, etc... just not sure I'm getting the format right. I watch it get to the page, I see the 'CHOOSE YOUR FEATURES' button on the page, but it never clicks it. Usually I get an error like this depending on which version was used:
unable to locate element, using {:text=>"CHOOSE YOUR FEATURES", :tag_name=>"button"}
This is the version I think should work, When I try it I get no errors but see nothing happen in the browser either:
$browser.div(:class => "personalizationButton").click
I'm not seeing anything inherently clickable (with output) in the HTML. With the IRB output, it shows that it is finding the div, but there is no action when clicking it. The <b> element is just the "bold" style, so we shouldn't need to try that.
If you can access the span with text CHOOSE YOUR FEATURES, that should be your button.
$browser.span(:text => "CHOOSE YOUR FEATURES").click
. . i have tried doing the same thing with godaddy.com buy it did work.
on the main page there is a button labeled "Buy Now" using the code below
brow.div(:class => 'gdhp-domain-link gdhp-rounded-corners').span(:text => 'Buy Now').click
I was

How can I automate a google link on the page I am working on using watir-webdriver?

Page link I am working on is http://www.whatcar.com/car-news/subaru-xv-review/260397
I am trying to automate 'clicking the google link' but am having no luck and keep receiving an error.
Link HTML:
<a tabindex="0" role="button" title="" class="s5 JF Uu" id="button" href="javascript:void(0);" aria-pressed="false" aria-label="Click here to publicly +1 this."></a>
My code:
#browser.link(:class, "s5 JF Uu").click
Error message:
unable to locate element, using {:class=>"s5 JF Uu", :tag_name=>"a"} (Watir::Exception::UnknownObjectException)
./step_definitions/11.rb:12:in `/^On the page I click 'Twitter' , Facebook and Google button$/'
11.feature:8:in `When On the page I click 'Twitter' , Facebook and Google+ button'
The link is inside a frame. To make it even more fun, frame id is different every time the page is refreshed.
browser.frames.collect {|frame| frame.id}
=> ["I1_1323429988509", "f3593c4f374d896", "f4a5e09c20624c", "stSegmentFrame", "stLframe"]
browser.refresh
=> []
browser.frames.collect {|frame| frame.id}
=> ["I1_1323430025052", "fccfdf9410ef34", "f11036dad706668", "stSegmentFrame", "stLframe"]
I1_1323429988509 and I1_1323430025052 is the frame. Since I1_ part is always the same, and no other frame has that, you can access the frame like this:
browser.frame(:id => /I1_/)
Since there is only one link inside the frame:
browser.frame(:id => /I1_/).as.size
=> 1
You can click the link like this:
browser.frame(:id => /I1_/).a.click
Or if you prefer to be more explicit
browser.frame(:id => /I1_/).a(:id => "button").click
That will open a new browser window, and a new challenge is here! :)
The technical answer:
The class of the button on the page that you linked is different for me than the class that you list. It looks like it behaves differently based on the cookies on your local machine (which would be absent during a Watir-driven Firefox or IE session).
You would need to find a different element that is not dynamic to hook into.
The ethical answer:
It is questionable that you are attempting to automate the promotion of online articles through social media. Watir/Watir-Webdriver is not a spam bot, and the services you are using specifically prohibit the use of automation/bots.
That 'button' link is inside an iframe. Read on the watir Wiki how to deal with stuff in frames. If that's not enough to get it working please edit the answer with revised code and error etc and we can work it forward from that point.

Resources