TimeoutException: Message:

Viewed 2772
import os   
from selenium import webdriver
import time    
from linkedin_scraper import actions    
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.common.exceptions import TimeoutException    
from selenium.webdriver.chrome.options import Options


chrome_options = Options()    
chrome_options.add_argument("--headless")

driver = webdriver.Chrome("driver/chromedriver", options=chrome_options)    
email = os.getenv("LINKEDIN_USER")    
password = os.getenv("LINKEDIN_PASSWORD")

actions.login(driver, email, password) # if email and password isnt given, it'll prompt in terminal
driver.get('https://www.linkedin.com/company/biorasi-llc/about/')

_ = WebDriverWait(driver, 3).until(EC.presence_of_all_elements_located((By.TAG_NAME, 'section')))

time.sleep(3)    
grid = driver.find_elements_by_tag_name("section")[3]
about_us = grid.find_elements_by_tag_name("p")[0].text.strip()

print(about_us)

This is my code for scraping about_us data of a company and it works but sometimes I get an error like:

TimeoutException Traceback (most recent call last) in

 17 email = os.getenv("LINKEDIN_USER")
 18 password = os.getenv("LINKEDIN_PASSWORD")

---> 19 actions.login(driver, email, password) # if email and password isnt given, it'll prompt in terminal

 20 driver.get('https://www.linkedin.com/company/biorasi-llc/about/')
 21 _ = WebDriverWait(driver, 3).until(EC.presence_of_all_elements_located((By.TAG_NAME, 'section')))

~\Anaconda3\lib\site-packages\linkedin_scraper\actions.py in login(driver, email, password)

 28   password_elem.submit()
 29 

---> 30 element = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "profile-nav-item")))

~\Anaconda3\lib\site-packages\selenium\webdriver\support\wait.py in until(self, method, message)

 78             if time.time() > end_time:
 79                 break

---> 80 raise TimeoutException(message, screen, stacktrace) 81 82 def until_not(self, method, message=''):

TimeoutException: Message:

Someone, please help how to solve this.

1 Answers

Probably because your timeout is too short (3 seconds) so before the page is fully loaded, it reaches timeout threshold. Try raising it to 5-10 seconds in line 21.

TIMEOUT = 10
 _ = WebDriverWait(driver, TIMEOUT).until(EC.presence_of_all_elements_located((By.TAG_NAME, 'section')))

Here's some tips to improve your code:

  • You're already using fluent wait (WebDriverWait), try to minimize using time.sleep if possible. WebDriverWait will stop waiting & return your element, so time will be saved.
  • Finding element via tag name & locate it by ordinal (in this case, 4th section tag) is a bad idea. Will break if the site adds more section. Try using better XPATH, here's my code, I have not tested but I think it will work just fine.
import os   
from selenium import webdriver
import time    
from linkedin_scraper import actions    
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.common.exceptions import TimeoutException    
from selenium.webdriver.chrome.options import Options


chrome_options = Options()    
chrome_options.add_argument("--headless")

driver = webdriver.Chrome("driver/chromedriver", options=chrome_options)    
email = os.getenv("LINKEDIN_USER")    
password = os.getenv("LINKEDIN_PASSWORD")

actions.login(driver, email, password) # if email and password isnt given, it'll prompt in terminal
driver.get('https://www.linkedin.com/company/biorasi-llc/about/')

# directly finds paragraph, removed time.sleep
paragraph_elem = WebDriverWait(driver, 15).until(EC.presence_of_element_located((By.XPATH, '//section//h4/..//p')))
about_us = paragraph_elem.text.strip()

print(about_us)
Related