Posting on LinkedIn Using Python and Selenium
Tags: testing, webdev, python, programming
Published: 2023-03-08 06:43:14

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".

{% embed https://youtu.be/FFwsQForJC4 %}

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

```python
# 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")
```

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.

```python
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)
```

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.

```python
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)
```

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.

```python
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()
```

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

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

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.

```python
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();
```

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.

```python
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)
```

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.

```python
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();
```

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.

```python
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 )
```

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