Download the File which has stream-url is the chrome extension in the embed tag using selenium webdriver in python

后端 未结 1 1307
清歌不尽
清歌不尽 2021-01-16 07:18

According to my code I have tried to click on the View button which contain the hidden document, I need to download that document using selenium webdriver in python. When I

1条回答
  •  孤城傲影
    2021-01-16 08:10

    In Firefox page uses to display PDF with scan. There are buttons in section "Uploaded Documents" to display other scans.

    This code uses these buttons to display scans, get data from and save in files document-0.pdf, document-1.pdf, etc.

    I use the same code you could see in my answer to your previous question:
    Save the pdf using the selenium webdriver in python

    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.common.keys import Keys
    import time
    
    url = 'https://maharerait.mahaonline.gov.in'
    
    #chrome_path = r'C:/Users/User/AppData/Local/Programs/Python/Python36/Scripts/chromedriver.exe'
    #driver = webdriver.Chrome(executable_path=chrome_path)
    
    driver = webdriver.Firefox()
    
    driver.get(url)
    
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH,"//div[@class='search-pro-details']//a[contains(.,'Search Project Details')]"))).click()
    registered_project_radio = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID,"Promoter")))
    driver.execute_script("arguments[0].click();", registered_project_radio)
    
    application = driver.find_element_by_id("CertiNo")
    application.send_keys("P50500000005")
    
    search = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID,"btnSearch")))
    driver.execute_script("arguments[0].click();", search)
    
    time.sleep(5)
    
    View = [item.get_attribute('href')
             for item in driver.find_elements_by_tag_name("a")
              if item.get_attribute('href') is not None]
    
    # if there is list then get first element
    if View:
        View = View[0]
    
    #-----------------------------------------------------------------------------
    
    # load page    
    driver.get(View)
    
    # find buttons in section `Uploaded Documents`
    buttons = driver.find_elements_by_xpath('//div[@id="DivDocument"]//button')
    
    # work with all buttons 
    for i, button in enumerate(buttons):
    
        # click button
        button.click()
    
        # wait till page display scan
        print('wait for object:', i)
        search = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.TAG_NAME, "object")))
    
        # get data from object    
        print('get data:', i)    
        import base64
    
        obj = driver.find_element_by_tag_name('object')
        data = obj.get_attribute('data')
        text = data.split(',')[1]
        bytes = base64.b64decode(text)
    
        # save scan in next PDF     
        print('save: document-{}.pdf'.format(i))    
        with open('document-{}.pdf'.format(i), 'wb') as fp:
            fp.write(bytes)
    
        # close scan        
        print('close document:', i)    
        driver.find_element_by_xpath('//button[text()="Close"]').click()    
    
    # --- end ---
    
    driver.close()
    

    0 讨论(0)
    提交回复
    热议问题