Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Unable to log in using selenium #486

Open
pvarshh opened this issue Jan 31, 2025 · 0 comments
Open

Unable to log in using selenium #486

pvarshh opened this issue Jan 31, 2025 · 0 comments

Comments

@pvarshh
Copy link

pvarshh commented Jan 31, 2025

i implemented arjun's solution into my code from pip install git+https://github.com/ArujAnand/linkedin-api@feature/authentication-using-cookie

when i manually grab the JSESSIONID and li_at values from the webpage, I am able to successfully use the api, however when trying to use selenium to automatically grab the JSESSIONID and li_at, i am hit with the following

WARNING:linkedin_api.client:ChallengeException encountered during authentication.
ERROR:linkedin_api.client:Failed to fetch cookies. Login may have failed or cookies are not available.
Traceback (most recent call last):
File "/Users/parney2004/Desktop/LinkedIn Bot/env/lib/python3.10/site-packages/requests/models.py", line 974, in json
return complexjson.loads(self.text, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/json/init.py", line 346, in loads
return _default_decoder.decode(s)
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/json/decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/json/decoder.py", line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/Users/parney2004/Desktop/LinkedIn Bot/scrape.py", line 11, in
profile = api.get_profile('pvarshh')
File "/Users/parney2004/Desktop/LinkedIn Bot/env/lib/python3.10/site-packages/linkedin_api/linkedin.py", line 745, in get_profile
data = res.json()
File "/Users/parney2004/Desktop/LinkedIn Bot/env/lib/python3.10/site-packages/requests/models.py", line 978, in json
raise RequestsJSONDecodeError(e.msg, e.doc, e.pos)
requests.exceptions.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

When i check the new webpage selenium opens, my username and password are correct, and I am able to use the SAME user / pass to log in on the regular browser, just not the mock browser selenium makes

def authenticate(self, username: str, password: str):
if self._use_cookie_cache:
self.logger.debug("Attempting to use cached cookies")
cookies = self._cookie_repository.get(username)
if cookies:
self.logger.debug("Using cached cookies")
self._set_session_cookies(cookies)
self._fetch_metadata()
return

    try:
        self._do_authentication_request(username, password)
    except ChallengeException:
        self.logger.warning("ChallengeException encountered during authentication.")
        
        # Use Selenium to automatically grab cookies
        self.logger.debug("Attempting to fetch cookies using Selenium...")
        
        # Initialize Selenium WebDriver
        driver = webdriver.Chrome()  # Ensure chromedriver is in your PATH
        try:
            # Open LinkedIn login page
            driver.get("https://www.linkedin.com/#")
            time.sleep(2)  # Wait for the page to load

            # Enter email and password
            email_field = driver.find_element(By.ID, "username")
            email_field.send_keys(username)

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

            # Submit the form
            password_field.send_keys(Keys.RETURN)
            time.sleep(5)  # Wait for login to complete

            # Extract cookies
            cookies = driver.get_cookies()

            # Find the li_at and JSESSIONID cookies
            li_at_cookie = next((cookie for cookie in cookies if cookie["name"] == "li_at"), None)
            jsessionid_cookie = next((cookie for cookie in cookies if cookie["name"] == "JSESSIONID"), None)

            if li_at_cookie and jsessionid_cookie:
                self.logger.debug("Cookies fetched successfully.")
                self.authenticate_with_cookies(jsessionid_cookie["value"], li_at_cookie["value"])
            else:
                self.logger.error("Failed to fetch cookies. Login may have failed or cookies are not available.")
                return

        finally:
            # Close the browser
            driver.quit()

    self._fetch_metadata()
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant