Search code examples
pythonphantomjssplinter

splinter: element not found inside iframe


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.


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