Below is an example Python solution that uses Selenium to navigate to the LinkedIn company posts page, scroll to load posts, and for each post “click” the “Copy link to post” menu item so that its URL can be grabbed (via the clipboard) and then saved into an Excel file. Note:
-
Disclaimer & Terms:
- This code is for educational/demonstration purposes only.
- Automated scraping or interacting with LinkedIn may violate their terms of service. Do not run this code against LinkedIn’s production site unless you have permission and understand the risks.
- You may need to adjust waits, selectors, and other details based on the actual page structure and your environment.
- When automating actions that modify the clipboard or simulate user actions, browser and OS security policies may come into play.
-
Prerequisites:
-
Approach Overview:
- The script opens LinkedIn’s login page then waits for you to log in manually.
- After logging in, it navigates to the EUIPO posts page.
- It scrolls multiple times so that all ~216 posts (from the specified period) load.
- For each post it finds (by looking for the “Copy link to post” option inside a dropdown menu), it does the following:
- Opens the dropdown menu (if needed).
- Clicks the “Copy link to post” option.
- Waits a moment then reads the clipboard (using the third‑party module
pyperclip) to obtain the copied URL.
- Closes/dismisses any open overlay if necessary.
- Finally, it writes all the collected links into an Excel file using
openpyxl.
Below is the full code sample:
import time
import pyperclip
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from openpyxl import Workbook
# -------------------------------
# CONFIGURATION & DISCLAIMER
# -------------------------------
LINKEDIN_LOGIN_URL = "https://www.linkedin.com/login"
EUIPO_POSTS_URL = "https://www.linkedin.com/company/euipo/posts/?feedView=all"
SCROLL_PAUSE_TIME = 2 # seconds to wait after scroll
# -------------------------------
# SETUP SELENIUM DRIVER
# -------------------------------
driver = webdriver.Chrome() # make sure chromedriver is in your PATH
driver.maximize_window()
wait = WebDriverWait(driver, 20)
# -------------------------------
# STEP 1: LOGIN (Manual)
# -------------------------------
driver.get(LINKEDIN_LOGIN_URL)
print("Please log in to your LinkedIn account in the opened browser window.")
input("After you have logged in, press Enter here to continue...")
# -------------------------------
# STEP 2: Navigate to the posts page
# -------------------------------
driver.get(EUIPO_POSTS_URL)
time.sleep(5) # give time for the page to load
# -------------------------------
# STEP 3: Scroll to load all posts
# -------------------------------
# For demonstration, we scroll several times. Adjust the number of scrolls according to your needs.
last_height = driver.execute_script("return document.body.scrollHeight")
for i in range(10): # adjust the range if needed to load ~216 posts
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
time.sleep(SCROLL_PAUSE_TIME)
new_height = driver.execute_script("return document.body.scrollHeight")
if new_height == last_height:
break
last_height = new_height
# -------------------------------
# STEP 4: Find and process each post
# -------------------------------
# We search for the "Copy link to post" button by looking for a button
# that has an h5 child with the text "Copy link to post".
#
# Note: The DOM structure can change. You might need to adjust the XPath.
#
# In many cases, the "Copy link to post" option is hidden inside a dropdown menu.
# So we first need to:
# a) Locate the menu button (often an ellipsis "...") on the post.
# b) Click it to open the dropdown.
# c) Then click on the "Copy link to post" option.
#
# For this example, we assume that each post has a container with the menu area.
# You may need to adapt the selectors according to the current LinkedIn DOM.
post_links = [] # this will store our URL strings
# Find all posts. Here we assume each post container has a specific CSS class.
# You might want to inspect the LinkedIn posts page and choose a more reliable selector.
posts = driver.find_elements(By.CSS_SELECTOR, "div.feed-shared-update-v2")
print(f"Found {len(posts)} posts on the page.")
for idx, post in enumerate(posts, start=1):
try:
# Scroll the post into view
driver.execute_script("arguments[0].scrollIntoView();", post)
time.sleep(1)
# Locate the three-dot menu button inside the post.
# This selector might need to be updated based on the DOM structure.
menu_button = post.find_element(By.CSS_SELECTOR, "button.feed-shared-control-menu__trigger")
# Sometimes the button is not directly clickable; use ActionChains for a safe click.
ActionChains(driver).move_to_element(menu_button).click(menu_button).perform()
time.sleep(1) # wait for the dropdown to appear
# Locate the "Copy link to post" option from the dropdown:
copy_link_option = wait.until(EC.element_to_be_clickable((
By.XPATH, ".//div[contains(@class,'feed-shared-control-menu__dropdown-item')]//h5[text()='Copy link to post']/ancestor::div[@role='button']"
)))
# Click the copy link option:
copy_link_option.click()
time.sleep(1) # allow clipboard to update
# Read the clipboard contents using pyperclip.
# Note: For this to work, the browser must allow clipboard writes.
link_url = pyperclip.paste()
if link_url:
print(f"[{idx}] Link collected: {link_url}")
post_links.append(link_url)
else:
print(f"[{idx}] No link found in clipboard.")
# Sometimes a modal or overlay is still open; press ESCAPE to close it, if needed.
webdriver.ActionChains(driver).send_keys("\ue00c").perform() # ESCAPE is "\ue00c"
time.sleep(1)
except Exception as e:
print(f"Error processing post #{idx}: {e}")
continue
print(f"Total links collected: {len(post_links)}")
# -------------------------------
# STEP 5: Write the links to an Excel file
# -------------------------------
workbook = Workbook()
sheet = workbook.active
sheet.title = "LinkedIn Post Links"
sheet.append(["Post Number", "Link"])
for i, link in enumerate(post_links, start=1):
sheet.append([i, link])
excel_filename = "linkedin_post_links.xlsx"
workbook.save(excel_filename)
print(f"Links saved to {excel_filename}")
# -------------------------------
# Clean up
# -------------------------------
driver.quit()