using watir for a facebook app - ruby

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 }

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

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.

Watir-Webdriver: I want to click on the last button with the same class or value

I am having trouble having my script work and finding a suitable solution.
The issue I am facing is I cannot get my code to go to the last button on the page and click on it. It seems like a trivial issue, but I am well and truly stumped. Can someone point me in the right direction?
Here is my code of what I have tried albeit with no success
#browser.button.last(:value => 'Unlock').click or #browser.button(:class => 'btn btn-danger').last.click
The error I keep getting is
undefined method 'last'
The reason why I cannot use an index in my code is because the number of buttons on the web page are not constant as the number of buttons on display can randomly increase or decrease. Hence my predicament.
You could use the buttons method to map all the buttons on the page and then click use the last method on the collection. For example:
buttons = b.buttons(:class => "class_name_here")
buttons.last.click

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.

How can I know what element is clicked in IE?

I'm automating IE with watir, and I want to know what html element(s) are clicked (selected). Can this be done using watir? win32ole? In a last chance, without ruby?
Something like:
Click a button -> button with id=213 and class=btn_class was clicked.
Click a text field -> text field with id=123 and value=my_text was clicked.
Try one of the recorders, Selenium IDE for example.
I'm not sure if I completely understand either, but would a custom function like this work?
def click(item, how, what)
#browser.item(how, what).click
puts "#{item} with #{how}->#{what} was clicked"
end
click("button", ":id", "awesome")
Edit: If you're attempting to identify page elements so that you can then use them in a Watir script, the Developer Toolbar is perfect for this application, much like Firebug for Firefox.
http://www.microsoft.com/download/en/details.aspx?id=18359
Your comment to me & your response to Zeljko appear to contradict each other. If you want to use WATIR for this task, the code above will execute a mouse click and post the information to console. The only other way to get information is to locate the object in WATIR and fish for more details:
object = #browser.button(:name, /myButton/)
object.id
object.title
object.status
object.height, etc.
or
#browser.divs.each do |div|
puts div.id, div.title
end
I'd recommend a strong look at the capabilities of the various developer tools, such as are provided with Chrome, Firefox, and IE. those are generally the best means to get this kind of information.
Watir is really about driving the browser, and getting info out of the DOM. it's not really setup to report on manual interactions with the browser.

Resources