Copy and print text inside iframe by watir webdriver - ruby

I want to copy text inside iframe by watir webdrive and print it. iframe is inside a div.
Both iframe and div have ID.
<div id="sitbReaderKindleSample" style="visibility: visible; width: 497.5px; height: 18138px;">
<iframe id="sitbReaderFrame" scrolling="no" frameborder="0" style="height: 17945px;">
</div>
my code is
targetURL = "https://www.amazon.in/Secret-Druids-Christopher-C-Doyle- ebook/dp/B01BMD4JF0?ie=UTF8&qid=1472664339&ref_=lp_1318158031_1_2&s=books&sr=1"
browser.goto targetURL
browser.image(:id => "ebooksSitbLogoImg").click
itext = browser.div(:id => 'sitbReaderKindleSample').text
puts itext
OUTPUT is
End of this sample Kindle book.
Enjoyed the preview?
Buy now
or even:
See details for this book in the Kindle Store
but i do not need this. This is from outside iframe. i need the text inside iframe.
I tried
itext = browser.iframe(:id => 'sitbReaderFrame').text
its output is blank
Need help to print the text inside the iframe
I use rubymine IDE

You are correct in doing:
browser.iframe(:id => 'sitbReaderFrame').text
If you are getting an empty String, that means you are using an older version of Watir:
In Watir-Webdriver v0.9.1 (and prior): iframe.text returned the text nodes of the iframe element, which is always nothing.
In Watir-Webdriver v0.9.2 (and later): iframe.text was changed to switch to the iframe context and then return its body text (which is what you want).
You should make sure that you are using the latest version.

itext = browser.iframe(:id => 'sitbReaderFrame').body.text
This works for me, adding the body to the chain.

Related

How to add more html to sweetalert after html to content update?

I saw previously you could edit the html portion of the sweetalert, but now that has been changed to content instead of doing raw html. Is there a way to go back to using html or another way of adding in an input. I saw in another post that it may be better to just use a modal instead.
https://github.com/sweetalert2/sweetalert2/issues/451 here is the guide with the html.
You are using SweetAlert2.
The plain HTML option still exists.
This is used as follows:
swal({
title: 'Title',
html: 'A custom <span style="color: #F8BB86">html<span> message.'
});
Javascript Sweet Alert and html link inside text

Problems uploading a file using file_field in Watir

My system:
Windows 10 Pro 64-bit
ruby 2.1.9p490 (2016-03-30 revision 54437) [x64-mingw32]
FireFox 47.0.1
To start, here is the code I'm dealing with:
<div class="dz-style col-sm-7" is="null">
<div is="null">You can drag and drop your supporting document files here, or click to select files to upload.</div>
<input style="display: none;" multiple="" is="null" type="file"></div>
Here is my watir testing code:
Identify and confirm file is valid
local_file = '/Users/tom.feodoroff/Desktop/Charlie_Snoopy.jpg'
File.exists? local_file
raise "error" unless File.exists? local_file
Change the style display so I can interact with control
element = BROWSER.input(:type => 'file')
puts element.attribute_value('style') #display: none;
script = "return arguments[0].style = 'display: inline'"
BROWSER.execute_script(script, element)
puts element.attribute_value('style') #display: inline;
Use suggested syntax to add file to application
BROWSER.file_field(:type => 'file').set(local_file)
This doesn't generate any errors, but it also doesn't attach the file so that my Submit button becomes active. Do I need a different version of Ruby (Watir) to make this work or is there something I'm missing?
I don't understand why, but I added sleep(5) just before clicking the submit button, and now it works. My file icon now appears on the page and the submit button is active and successfully submitted the form. Things that make you go 'hmmmm' :)
Thanks for the responses. Hopefully this will help someone else with this problem?

Pasting large text into a Watir Webdriver textfield in Ruby

I have a text field that I am trying to manipulate with Watir Webdriver in Ruby, which is in this format:
<div class="fieldwrapper" ng-hide="tempPageContent.eulaModal.standardEula">
<label class="ng-binding">Custom License Agreement</label>
<textarea ng-model="tempEula.EULAText" class="med ng-pristine ng-valid"></textarea>
<!-- <span text-area-with-counter="tempEula.EULAText"
text-limit="{{ referenceData.appMetaDataReference.maxEulaChars }}"
text-area-class="med"
text-area-required="tempPageContent.eulaModal.customEula"
text-area-itc-field-server-error="versionInfo.eula.errorKeys"
text-area-itc-field-orig-val="orignalVersionInfo.eula.EULAText"
text-area-itc-field-cur-val="tempEula.EULAText"
text-area-itc-empty-errormsg="Enter the license agreement"
text-area-itc-char-exceed-errormsg="The license agreement can not exceed {{ referenceData.appMetaDataReference.maxEulaChars }} characters"></span> -->
</div>
I need to insert a large String that is extracted from a text file into this text field, but using the standard Watir.textarea.set won't work as it would time-out in 30 seconds. Here is what I am trying to do at the moment:
#browser.execute_script("arguments[0].value = arguments[1]", text_field, eula_text)
which injects the text into the text field, but does not enable the 'Save' button, which is triggered by the native set method, but not by Javascript.
I saw some post by jarib, who proposed the usage of Mac's pbcopy for copying text and then using the send_keys([:command, 'v']), but using the send_keys does not work, although the text is in the IO buffer. I tried both open and popen methods. I also tried using pbpaste on Watir textarea element...
I cannot think of a novel idea to accomplish my task and any pointers in the right direction would be appreciated. I am just not familiar with the way AngularJS text fields work with text input.
I am using the latest watir-webdriver 0.9.1 and chromedriver.
3 possibilities:
1) Increase the client timeout:
client = Selenium::WebDriver::Remote::Http::Default.new
client.timeout = 180
b = Watir::Browser.new :chrome, http_client: client
2) Don't do all the text at once:
File.open('xxx.txt').each do |line|
textfield.append(line)
end
3) Use your javascript code to copy everything in, then use textfield.append(' ') to enable the save button.

CKEditor moving br tags

I'm having a problem with CKEditor changing my original paragraph formatting with negative side effects.
I start with a basic paragraph loaded into CKEditor using setData():
<p><span style="font-size:50px">My Text</span></p>
... more document content ...
In the editor, I move the cursor to the end of the phrase "My Text" and press enter (with config.enterMode=CKEDITOR.ENTER_BR setting enabled). Inspecting the markup inside the editor I now see:
<p><span style="font-size:50px">My Text<br><br></span></p>
... more document content ...
Then, when I call getData() to pull the contents from the editor and save the document to a database, the HTML extracted by getData() looks like this:
<p><span style="font-size:50px">My Text</span><br> </p>
... more document content ...
This is a problem because while editing, the <br> tag was inside the <span> and was subject to the 50px font size style. The user saw a 50px blank line before the next piece of document content. After saving the HTML to a database and reloading later the <br> tag is now outside the <span> and is not subject to the 50px font sizing and the blank line appears much smaller than before.
The round trip fidelity of the text formatting is not preserved and the user is frustrated by the results.
Can someone help me understand the results I'm seeing with <br> tags being reformatted and moved around during the editing life cycle, and how I might fix this problem?
Using CKEditor v4.4.1

watir webdriver get into iframe and get src of elements and click on link

With Watir-Webdriver I want to go change the focus to the iframe and get the link that is inside it.
Here is the html code
<iframe id="top_right" src="otherwebsite.com/need content src">
<img src="need this" />
So what I would like is to go into the iframe, get the src of it, capture the href and the src from the img element and in the end click on these elements retrieving the data.
This is my attempt using Ruby:
require 'watir-webdriver'
b = Watir::Browser.new
b.goto 'somesite.com'
b.wait
f = b.frame(:id => 'top_right').link(:index => 1).click
I have got until here but unfortunately i still get the following response:
in `assert_exists': unable to locate element, using {:index=>1, :tag_name=>"a"} (Watir::Exception::UnknownObjectException)
so if anybody have some help it would be great tnx.
You are trying to click the second link (:index=>1) in the frame. Looks like the frame does not have two links. Try clicking the first link (:index=>0):
b.frame(:id => 'top_right').link(:index => 0).click

Resources