DEV Community

Cover image for Posting on LinkedIn Using Python and Selenium
Furkan Gulsen
Furkan Gulsen

Posted on

5

Posting on LinkedIn Using Python and Selenium

Greetings to everyone, in this article, I will explain the process of sharing a post on LinkedIn for my University course "Software Quality Assurance and Testing".

Selenium checks if the page is displayed correctly by logging into linkedin.com.

# Opening the web page with ChromeDrive
self.driver.get("https://www.linkedin.com")
# Testing if the site with the correct URL loads
self.assertLessEqual(self.driver.title, "Log In or Sign Up")
Enter fullscreen mode Exit fullscreen mode

After the login page is opened, it is checked whether there is an email input field on the page. Then, the email value given is written into the input field.

email_elem = self.driver.find_element(By.ID, 'session_key')
# Check if email input field is accessible
self.assertTrue(email_elem.is_enabled())
# Filling in the Email input field
email_elem.send_keys(self.email)
Enter fullscreen mode Exit fullscreen mode

After the login page is opened, it is checked whether there is a password input field on the page. Then the given password value is written into the input field.

password_elem = self.driver.find_element(By.ID, 'session_password')
# Check if the password input field is accessible
self.assertTrue(password_elem.is_enabled())
# Filling the password input field
password_elem.send_keys(self.password)
Enter fullscreen mode Exit fullscreen mode

After the two input fields on the login page are filled, it is tested whether the "LOG IN" button can be accessed. Then, if the input fields are full, the button is clicked.

login_button = self.driver.find_element(By.CSS_SELECTOR,
'.sign-in-form__submit-button')
self.assertTrue(login_button.is_displayed())
self.assertTrue(login_button.is_enabled())
login_button.click()
Enter fullscreen mode Exit fullscreen mode

After the login is successful, you will be redirected to LinkedIn's homepage. Checking whether the home page is displayed correctly.

home_page = self.driver.find_element(By.ID,"voyager-feed")
# check if home page is opened
self.assertTrue(home_page.is_displayed())
Enter fullscreen mode Exit fullscreen mode

It is checked whether the button on the homepage for sharing a post is displayed and whether it is accessed later. If there is no problem, the button is clicked.

add_post_button =
self.driver.find_element(By.CSS_SELECTOR,'.artdeco-button.artdeco-button
--muted.artdeco-button--4.artdeco-button--tertiary.ember-view.share-box-
feed-entry__trigger')
self.assertTrue(add_post_button.is_displayed())
self.assertTrue(add_post_button.is_enabled())
add_post_button.click();
Enter fullscreen mode Exit fullscreen mode

After clicking the button, a modal with an input field and a button to share a post opens on the LinkedIn page. After checking whether the modal is accessible or not, the specified share text is written into the input.

time.sleep( 2 )
self.postText = self.postText + str(random.randint( 0 , 100000 ))
post_text_area =
self.driver.find_element(By.CSS_SELECTOR,".ql-editor.ql-blank")
self.assertTrue(post_text_area.is_enabled())
post_text_area.send_keys(self.postText)
Enter fullscreen mode Exit fullscreen mode

It is checked whether the button in the modal is displayed or not, and whether it can be accessed later. If there is no problem, the button is clicked and the post is shared.

time.sleep( 2 )
post_send_button =
self.driver.find_element(By.CSS_SELECTOR,'.share-actions__primary-action
.artdeco-button.artdeco-button--2.artdeco-button--primary.ember-view')
self.assertTrue(post_send_button.is_displayed())
self.assertTrue(post_send_button.is_enabled())
post_send_button.click();
Enter fullscreen mode Exit fullscreen mode

After pressing the share post button, the modal closes. Then, whether the post is shared or not is checked by checking the text in the relevant post.

time.sleep( 2 )
post_content_div =
self.driver.find_element(By.CSS_SELECTOR,'.break-words>span')
self.assertTrue(post_content_div.is_displayed())
self.assertEqual(self.postText, post_content_div.text)
time.sleep( 3 )
Enter fullscreen mode Exit fullscreen mode

GitHub Repo: https://github.com/Furkan-Gulsen/python-selenium-test-islemleri

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (1)

Collapse
 
amyliumaiyi profile image
Amy Liu • Edited

This was exactly what I was looking for. thansk for sharing

The Most Contextual AI Development Assistant

Pieces.app image

Our centralized storage agent works on-device, unifying various developer tools to proactively capture and enrich useful materials, streamline collaboration, and solve complex problems through a contextual understanding of your unique workflow.

👥 Ideal for solo developers, teams, and cross-company projects

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay