Search code examples
rubywatir-webdriver

Copy and print text inside iframe by watir webdriver


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

enter image description here


Solution

  • 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.