0

I am trying to scrape webpages using python and selenium. I have a url which takes a single parameter and a list of valid parameters. I navigate to that url with a single parameter at a time and click on a link, a pop up window opens with a page.

The pop window automatically opens a print dialogue on page load.
Also the url bar is disabled for that popup.

My code:

def packAmazonOrders(self, order_ids):
    order_window_handle = self.driver.current_window_handle     
    for each in order_ids:
        self.driver.find_element_by_id('sc-search-field').send_keys(Keys.CONTROL, "a")
        self.driver.find_element_by_id('sc-search-field').send_keys(Keys.DELETE)
        self.driver.find_element_by_id('sc-search-field').send_keys(each)
        self.driver.find_element_by_class_name('sc-search-button').click()
        src = self.driver.page_source.encode('utf-8')
        if 'Unshipped' in src and 'Easy Ship - Schedule pickup' in src:
            is_valid = True
        else:
            is_valid = False

        if is_valid:
            print 'Packing Slip Start - %s' %each
            self.driver.find_element_by_link_text('Print order packing slip').click()
            handles = self.driver.window_handles
            print handles
            try:
                handles.remove(order_window_handle)
            except:
                pass
            self.driver.switch_to_window(handles.pop())
            print handles
            packing_slip_page = ''
            packing_slip_page = self.driver.page_source.encode('utf-8')
            if each in packing_slip_page:
                print 'Packing Slip Window'
            else:
                print 'not found'
            self.driver.close()
            self.driver.switch_to_window(order_window_handle)

Now I have two questions:

  1. How can I download that pop up page as pdf?
  2. For first parameter every thing works fine. But for another parameters in the list the packing_slip_page does not update (which i think because of the disabled url bar. But not sure though.) I tried the print the handle (print handles) for each parametre but it always print the same value. So how to access the correct page source for other parameters?
Manish Gupta
  • 4,438
  • 18
  • 57
  • 104
  • The print pop up is windows alert not a web based alert. So you can use Robot class to overcome it. Or you can switch from driver to alert and then accept it. I think Robot will help you. – Kishan Patel May 13 '16 at 09:06
  • @KishanPatel How to do this in Python? I looked on internet but couldn't find any example. Can you elaborate it a little bit? – Manish Gupta May 13 '16 at 09:14
  • Hi Manish, I'm a java guy i don't know python much. Will it help if i show you some code based on java and then you can relate it to your syntax. Will it be fine for you ? – Kishan Patel May 13 '16 at 09:33
  • Sure. anything can be of use.. Thanx. – Manish Gupta May 13 '16 at 09:33
  • Hey, i got something for you... http://stackoverflow.com/questions/860013/is-there-a-python-equivalent-to-javas-awt-robot-class – Kishan Patel May 13 '16 at 09:34
  • Just see this first.. IF this solves your problem then it's good, or else i will show in java. Do reply :-) Happy learning. – Kishan Patel May 13 '16 at 09:35

0 Answers0