How to Download pdf using python selenium in google colab

Viewed 549
!pip install selenium                      
!apt-get update  
!apt install chromium-chromedriver
i=1                                                                            
from selenium import webdriver           
chrome_options = webdriver.ChromeOptions()           
chrome_options.add_argument('--headless')   
chrome_options.add_argument('--no-sandbox')        
chrome_options.add_argument('--disable-dev-shm-usage')            
import requests     
driver =webdriver.Chrome('chromedriver',chrome_options=chrome_options)                      
driver.get("""https://www.faa.gov/airports/runway_safety/diagrams/""")  #Open website
search=driver.find_element_by_xpath("""//*[@id="ident"]""") #It will go to search bar       
ICAO=input("ENTER 4 DIGIT ICAO CODE To Retrive Airport Diagram\t")      
search.send_keys(ICAO)# It will send input to search box            
driver.find_element_by_xpath("""//*[@id="searchDtpp"]/fieldset/div[4]/input""").click()  
#Search button    
links=driver.find_element_by_xpath("""//*[@id="resultsTable"]/tbody/tr/td[8]/a""") #Find 
location of diagram                     
r = requests.head(links.get_attribute('href')) #give link of pdf                               
link=links.get_attribute('href')   
print(link)                   
1 Answers

The problem is webdriver setup in google colab. use this code instead.

!pip install selenium
!apt-get update # to update ubuntu to correctly run apt install
!apt install chromium-chromedriver
!cp /usr/lib/chromium-browser/chromedriver /usr/bin
import sys
sys.path.insert(0,'/usr/lib/chromium-browser/chromedriver')
from selenium import webdriver
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')
driver = webdriver.Chrome('chromedriver',chrome_options=chrome_options)

Edit:

To save in your google drive use this code:

  • mount google drive
from google.colab import drive
drive.mount('/content/gdrive')
  • save pdf file
response = requests.get(link)
path = "gdrive/My Drive/" + "filename" + ".pdf"
with open(path, "wb") as file:
  file.write(response.content)
Related