Search code examples
pythonseleniumselenium-webdriverxpathwebdriverwait

Login and Logout of LinkedIn using Selenium


I wish to write a program that Logs in and Logs out of LinkedIn in python using Selenium.

I have been able to achieve the Login feature but I cannot really understand how to Log out. I tried to find a solution but best I could reach is

Can someone help me out please?

driver = webdriver.Chrome(executable_path="C:/Users/Asus/Desktop/Project_Exhibition II/Project_Exhibition_2/chromedriver.exe")
driver.get("https://www.linkedin.com/login?fromSignIn=true&trk=guest_homepage-basic_nav-header-signin")
time.sleep(5)

username = driver.find_element(By.ID, "username")
username.send_keys("example@linkedIn.com")
pword = driver.find_element(By.ID, "password")
pword.send_keys("password\n")

time.sleep(15)

dropdownButton = driver.find_element(By.CSS_SELECTOR, 'global-nav__primary-link')
dropdownButton.click()
signoutButton = driver.find_element_by_xpath('//*[@href="/m/logout/"]')
signoutButton.click()

driver.quit()

It Logs in fine but then abruptly closes the browser.


Solution

  • The logout button just redirects you to "/m/logout".

    '//*[@href="/m/logout/"]'

    So you can just run:

    driver.get("https://linkedin.com/m/logout")