</div>
</div>
<div class="flex-grow-1 text-align-left">
<h5 class="feed-shared-control-menu__headline t-14 t-black t-bold" role="none">
Copy link to post
</h5>
<p class="feed-shared-control-menu__sub-headline t-12 t-black t-black--light">
</p>
</div>
and click it and copy a link to an external excel file, please there is around 216 posts from January 2025 to July 2025. go through each of these posts and in each case click the button and copy the link and save it in a file.
Assistant
Of course. Here is a complete Python code for this task.
Important Disclaimers Before You Start
Educational Use Only: This script is for educational purposes to demonstrate how browser automation works. Automating interactions with websites like LinkedIn can be against their User Agreement. Use this responsibly and at your own risk.
LinkedIn Login Required: LinkedIn does not allow viewing of most company post data without being logged in. This script requires you to provide your LinkedIn email and password to work.
Dynamic Web Pages: LinkedIn's website structure changes frequently. The element identifiers (like classes and IDs) used in this script might break in the future. The ember ID you provided is a perfect example of a dynamic ID that changes every time you load the page, so we cannot use it directly. The code below uses more stable methods to find the elements.
Future Dates: The date range you mentioned, "January 2025 to July 2025," is in the future. The script is designed to scroll down the page and collect all available posts it can load. Filtering by a specific date range is a much more complex task as the text of the date is not always easily accessible without complex parsing. This script will get the links for all posts it can find by scrolling.
A Better, More Reliable Method
Instead of clicking the three-dots menu and then the "Copy link to post" button for every single post (which is slow and can be unreliable), there is a much more efficient way.
Each post on LinkedIn has a unique identifier stored in a data attribute called data-urn. We can extract this URN directly and build the post's URL from it. This is significantly faster and more stable than simulating clicks. The code below uses this superior method.
Prerequisites
You need to have Python installed, along with the Selenium library.
Install Libraries: Open your terminal or command prompt and run this command:
bash
pip install selenium webdriver-manager pandas
selenium: The main library for browser automation.
webdriver-manager: Automatically downloads and manages the correct browser driver for you.
pandas: Used here to easily save the data to an Excel file.
Provide Your Credentials: You will need to enter your LinkedIn email and password when the script prompts you.
Do not share this code with your credentials saved in it.
The Python Code
Copy and paste the following code into a file named linkedin_scraper.py.
python
import time
import getpass
import pandas as pd
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.common.exceptions import NoSuchElementException
# --- Configuration ---
COMPANY_URL = "https://www.linkedin.com/company/euipo/posts/?feedView=all"
OUTPUT_FILE = "linkedin_post_links.xlsx"
SCROLL_PAUSE_TIME = 2.5 # How long to wait between scrolls (in seconds)
MAX_SCROLLS = 30 # Limit the number of scrolls to avoid infinitely running. Adjust as needed.
def login_to_linkedin(driver, email, password):
"""Navigates to LinkedIn login page and signs in."""
print("Navigating to LinkedIn login page...")
driver.get("https://www.linkedin.com/login")
time.sleep(2)
try:
print("Entering credentials...")
# Find email and password fields and enter credentials
email_field = driver.find_element(By.ID, "username")
email_field.send_keys(email)
password_field = driver.find_element(By.ID, "password")
password_field.send_keys(password)
# Click the sign-in button
password_field.send_keys(Keys.RETURN)
print("Login successful.")
time.sleep(5) # Wait for the main page to load
except NoSuchElementException:
print("Could not find login fields. The page structure may have changed.")
driver.quit()
exit()
def scrape_post_links(driver, url):
"""Scrolls through the company page and scrapes post URNs to build URLs."""
print(f"Navigating to company page: {url}")
driver.get(url)
time.sleep(4) # Allow time for the initial page load
# A set to store unique post links to avoid duplicates
post_links = set()
print("Starting to scroll and scrape posts...")
last_height = driver.execute_script("return document.body.scrollHeight")
scroll_count = 0
while scroll_count < MAX_SCROLLS:
# Find all post elements on the currently visible page
# The key is finding elements with a 'data-urn' attribute starting with 'urn:li:activity:'
posts = driver.find_elements(By.CSS_SELECTOR, "div[data-urn*='urn:li:activity:']")
if not posts:
print("No posts found. Waiting and trying again...")
time.sleep(SCROLL_PAUSE_TIME)
for post in posts:
try:
# Extract the URN (Uniform Resource Name)
urn = post.get_attribute("data-urn")
if urn:
# Construct the full, shareable post link
full_link = f"https://www.linkedin.com/feed/update/{urn}"
post_links.add(full_link)
except Exception as e:
print(f"Could not process a post element: {e}")
# Scroll down to the bottom of the page
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
scroll_count += 1
print(f"Scroll #{scroll_count}/{MAX_SCROLLS}. Found {len(post_links)} unique links so far.")
# Wait for new content to load
time.sleep(SCROLL_PAUSE_TIME)
# Check if we have reached the bottom of the page
new_height = driver.execute_script("return document.body.scrollHeight")
if new_height == last_height:
print("Reached the bottom of the page or no new content loaded.")
break
last_height = new_height
return list(post_links)
def save_to_excel(links, filename):
"""Saves a list of links to an Excel file."""
if not links:
print("No links were found to save.")
return
print(f"Saving {len(links)} links to {filename}...")
# Create a DataFrame using pandas
df = pd.DataFrame(links, columns=['Post URL'])
# Write the DataFrame to an Excel file
df.to_excel(filename, index=False)
print("File saved successfully.")
if __name__ == "__main__":
# Get user credentials securely
linkedin_email = input("Enter your LinkedIn Email: ")
linkedin_password = getpass.getpass("Enter your LinkedIn Password: ")
# Setup Selenium WebDriver
service = Service(ChromeDriverManager().install())
driver = webdriver.Chrome(service=service)
driver.implicitly_wait(10) # Global wait for elements to appear
try:
login_to_linkedin(driver, linkedin_email, linkedin_password)
# Check if login was redirected (e.g., due to a security check)
if "feed" not in driver.current_url and "login" in driver.current_url:
print("\nLogin may have failed or requires a security check.")
print("Please complete the security check in the browser window.")
input("Press Enter here after you have logged in manually...")
all_links = scrape_post_links(driver, COMPANY_URL)
save_to_excel(all_links, OUTPUT_FILE)
finally:
print("Closing the browser.")
driver.quit()
How to Run the Code
Save the code as linkedin_scraper.py.
Open your terminal or command prompt.
Navigate to the directory where you saved the file.
Run the script with the command: python linkedin_scraper.py
The script will prompt you to enter your LinkedIn email and password in the terminal.
A new Chrome browser window will open and perform the automation steps. You will see it log in, navigate to the EUIPO company page, and start scrolling.
Once it's finished (either by reaching the bottom of the feed or hitting the MAX_SCROLLS limit), it will create a file named linkedin_post_links.xlsx in the same directory.
Output
The linkedin_post_links.xlsx file will contain a single column named "Post URL" with all the links it found, like this: