Question

I am trying to click on a link called 'Estimate Shipping Fee' from http://www.dresslink.com/women-woolen-winter-trench-double-button-fur-collar-coat-p-9442.html that will open up an iframe overlay that fetches content via ajax and populates it.

here's the code

from splinter import Browser

browser = Browser('phantomjs')

def extract(url):
        browser.visit(url)
        browser.find_by_css(".floatl.shipping_fee a").click()
        browser.is_element_present_by_id('global_popup_login_iframe', wait_time=10)
        with browser.get_iframe('global_popup_login_iframe') as iframe:
                iframe.is_element_present_by_id('dyanmic_shipping_list', wait_time=10)
                shippingprice = iframe.find_by_tag('img')
                print shippingprice[1]['src']

extract('http://www.dresslink.com/women-woolen-winter-trench-double-button-fur-collar-coat-p-9442.html'

the problem is that it seems to not able to find an element inside the iframe, I've tried find_by_css, by_xpath, the result is same.

splinter.exceptions.ElementDoesNotExist: no elements could be found with tag_name "img"

there's an img tag in there for sure, yet it cannot find it.

Was it helpful?

Solution

I now found that the dyanmic_shipping_list is inside a div with id="shipping_rates" and not within the iframe global_popup_login_frame. Searching within the iframe therefore will give no result, hence the error. But calling

browser.find_by_id("dyanmic_shipping_list").find_by_tag("img")[1]['src']

results in

u'http://misc.dresslink.com/nap/images/images_common/icon-china.jpg'

is that what you want?

EDIT: I used firefox as a webbrowser instead of phantomjs. The commands are mainly the same for both, so you can develop your program using firefox and later make it work with phantomjs. With firefox you are also able to inspect the page with plugins like firebug, what will save you a huge amount of time

OTHER TIPS

It's not well documented so it takes a little time to figure out how to go about it, I was stuck with the same problem last night so I'm posting my method here. Splinter has a very easy workaround for iframes!

with br.get_iframe('Name_of_iframe') as iframe:
    iframe.fill("Name_of_element_to_be_filled","Text_to_be_filled")

In my case, the element's name was a randomly generated string, so the above could not be used. If you face such an issue do this, find it by the tag (say input in my case), count from the page source what number is it in the order of input tags inside the iframe. Then you can use this:-

with br.get_iframe('Name_of_iframe') as iframe:
    iframe.find_by_tag("tag_to_search")[index_number].fill(text_to_fill)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top