Form Data is not submitting to LinkedIn Using Python Requests and Selenium

I’m trying to automate updating my LinkedIn profile using Python. I’ve successfully logged in using Selenium, and now I’m attempting to use the requests library to submit form data for just the “About” section. However, I’m consistently getting a 403 Forbidden error when attempting to submit the form.

Here’s what I’ve done so far:

Login with Selenium:
I can successfully log in to LinkedIn using Selenium and navigate to my profile page.

Extract Cookies and CSRF Token:
I extracted cookies from the Selenium session and obtain the CSRF token from the profile page’s HTML.

Submit Form Data with Requests:
I attempted to submit the “About” section form data using the requests library, including cookies and the CSRF token.

But no matter what I change, I keep receiving a 403 Forbidden response.

My code:

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import requests
import json
import re

username = 'my_username'
password = 'my_password'
options = webdriver.ChromeOptions()
# options.add_argument('--headless')  # for headless mode
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))

def get_cookies(driver):
    return {cookie['name']: cookie['value'] for cookie in driver.get_cookies()}

def extract_csrf_token(html):
    match = re.search(r'name="csrfToken" content="(.*?)"', html)
    return match.group(1) if match else None

def about(session, link, val, csrf_token, headers):
    summary_field_name = 'gai-text-form-component-profileEditFormElement-SUMMARY-profile-ACoAAE9YpSsBDmNZhmxG3ss87KP-i8rqiD6k-gM-summary'
    data = {
        summary_field_name: val,
        'save_button_name': 'Save',
        'csrfToken': csrf_token  
    }
    response = session.post(link, data=data, headers=headers)
    if response.status_code == 200:
        print("Form submitted successfully!")
    else:
        print(f"Failed to submit form: {response.status_code}")
        print(response.text)

try:
    driver.get('https://www.linkedin.com/login')
    username_field = driver.find_element(By.ID, 'username')
    username_field.send_keys(username)

    password_field = driver.find_element(By.ID, 'password')
    password_field.send_keys(password)

    password_field.send_keys(Keys.RETURN)

    WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.XPATH, '//a[@class="ember-view block"]'))
    ).click()

    profile_page = driver.current_url
    driver.get(profile_page)
    
    csrf_token = extract_csrf_token(driver.page_source)

    base = "https://www.linkedin.com/in/timmy-scrown-a6399b311/edit/forms/{}/new/?profileFormEntryPoint=PROFILE_COMPLETION_HUB"
    cookies = driver.get_cookies()
    session = requests.Session()

    for cookie in cookies:
        session.cookies.set(cookie['name'], cookie['value'])

    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36',
        'Content-Type': 'application/x-www-form-urlencoded',
        'Referer': profile_page,
        'X-CSRF-Token': csrf_token,
        'X-Requested-With': 'XMLHttpRequest'
    }

    sections = {
        "about": "summary",
        "education": "education",
        "positions": "position",
        "career_break": "career-break",
        "skills": "skills",
        "certifications": "certification",
        "projects": "project",
        "courses": "course",
        "volunteer_experience": "volunteer-experience",
        "publications": "publication",
        "patents": "patent",
        "honors_awards": "honor",
        "languages": "language",
        "organizations": "organization",
        "test_scores": "test-score",
        "causes": "causes"
    }

    functions = {
        "about": about,
        "education": education,
        "positions": positions,
        "career_break": career_break,
        "skills": skills,
        "certifications": certifications,
        "projects": projects,
        "courses": courses,
        "volunteer_experience": volunteer_experience,
        "publications": publications,
        "patents": patents,
        "honors_awards": honors_awards,
        "languages": languages,
        "organizations": organizations,
        "test_scores": test_scores,
        "causes": causes
    }

    with open('core.json', 'r') as file:
        data = json.load(file)

    for key, value in data.items():
        if key in functions:
            link = base.format(sections[key])
            functions[key](session, link, value, csrf_token, headers)
            print(link, key, value)

finally:
    driver.quit()

core.json

{
  "about": "Experienced software engineer with a passion for developing innovative programs.",
  "education": [
    {
      "school": "University of Technology",
      "degree": "Bachelor of Science in Computer Science",
      "field_of_study": "Computer Science",
      "start_year": "2016",
      "end_year": "2020"
    }
  ],
  "positions": [
    {
      "title": "Senior Developer",
      "company": "TechCorp",
      "location": "New York, NY",
      "start_month": "January",
      "start_year": "2021",
      "end_month": "Present",
      "description": "Leading a team of developers to create innovative software solutions."
    }
  ],
  "skills": ["Python", "Java", "JavaScript"],
  "certifications": [
    {
      "name": "Certified Java Developer",
      "issuing_organization": "Oracle",
      "issue_date": "2021-06"
    }
  ]
}

My Error

Failed to submit form: 403
<!DOCTYPE html>
<html lang="en">
    <head>
        <title>Unauthorized</title>
        <style>
            html, body, pre {
                margin: 0;
                padding: 0;
                font-family: Monaco, 'Lucida Console', monospace;
                background: #ECECEC;
            }
            h1 {
                margin: 0;
                background: #333;
                padding: 20px 45px;
                color: #fff;
                text-shadow: 1px 1px 1px rgba(0,0,0,.3);
                border-bottom: 1px solid #111;
                font-size: 28px;
            }
            p#detail {
                margin: 0;
                padding: 15px 45px;
                background: #888;
                border-top: 4px solid #666;
                color: #111;
                text-shadow: 1px 1px 1px rgba(255,255,255,.3);
                font-size: 14px;
                border-bottom: 1px solid #333;
            }
        </style>
    </head>
    <body>
        <h1>Unauthorized</h1>
        <p id="detail">
            You must be authenticated to access this page.
        </p>
    </body>
</html>

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật