3

I am trying to automate posting into my social media account. I have successfully written the script to log in, after login in I am having difficulty locating the textarea element with which to pass my post, after which I will try to attach an image to my post, make the post and log out. But for now, I am stuck at the locating the textarea after login in. This is the code

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
usernameStr = 'JonJames3872@gmail.com'
passwordStr = 'JamesJon'
textStr = 'Testing my Post'
browser = webdriver.Chrome()
browser.get(('https://accounts.kingsch.at/?client_id=com.kingschat&scopes=%5B%22kingschat%22%5D&redirect_uri=https%3A%2F%2Fweb.kingsch.at%2F'))
# fill in username and password


password = browser.find_element_by_name('password')
password.send_keys(passwordStr)


username = browser.find_element_by_class_name('field')
username.send_keys(usernameStr)

signInButton = browser.find_element_by_class_name('submit-btn')
signInButton.click()

# I HAVE LOGGED IN, NOW THIS IS WHERE MY CODE HAS A PROBLEM

text = browser.find_element_by_xpath("/html/body/div[1]/div/div[2]/div/div[2]/div/div[1]/div/div/div[1]/textarea")
text.send_keys(textStr)

This is the element, from inspect element:

<textarea placeholder="What's happening?" class="KingingBox__input"></textarea>

Textarea HTML Screenshot

Notification Screenshot

dpapadopoulos
  • 1,834
  • 5
  • 23
  • 34
Jon James
  • 33
  • 4

1 Answers1

1
  1. I inserted code to write something in the text area
  2. I inserted code to upload a photo
  3. I inserted code to post your own post

I think 3 out of the 3 questions are all done.

Try this code:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
import os


path = '/home/avionerman/Documents/stack'
browser = webdriver.Firefox(path)
browser.implicitly_wait(10)


usernameStr = 'JonJames3872@gmail.com'
passwordStr = 'JamesJon'
textStr = 'Testing my Post'
browser.get(('https://accounts.kingsch.at/?client_id=com.kingschat&scopes=%5B%22kingschat%22%5D&redirect_uri=https%3A%2F%2Fweb.kingsch.at%2F'))
# fill in username and password


password = browser.find_element_by_name('password')
password.send_keys(passwordStr)


username = browser.find_element_by_class_name('field')
username.send_keys(usernameStr)

signInButton = browser.find_element_by_class_name('submit-btn')
signInButton.click()


text = browser.find_element_by_class_name('KingingBox__input')
text.clear()
text.send_keys(textStr)

time.sleep(5)

image = browser.find_element_by_class_name('KingingBox__attachment-input').send_keys('/here/path/of_yours/th_574e7c36606306d94a4.jpg')
time.sleep(5)


inserted_photo = browser.find_element_by_class_name('KingingBox__attachments-list')
if inserted_photo.is_displayed():
  print("Element found, photo uploaded successfully")
  browser.find_element_by_css_selector('.KingingBox__submit-btn').click()
else:
  print("Element not found")

At this line:

browser.implicitly_wait(10)

we determine that the browser will wait a max time of 10 seconds for each element to be visible. If the textarea will make more than 10 seconds to appear, the the script will stop. If you see enormous delays increase the seconds of waiting.

Also, as you can see I used this line:

text = browser.find_element_by_class_name('KingingBox__input')

in order to locate the textarea.

In this line:

image = browser.find_element_by_class_name('KingingBox__attachment-input').send_keys('/here/path/of_yours/th_574e7c36606306d94a4.jpg')

I locate the input tag which is responsible for accepting uploades and then I send on it the exact path of the file that I want to upload.

In the last part:

inserted_photo = browser.find_element_by_class_name('KingingBox__attachments-list')
if inserted_photo.is_displayed():
  print("Element found, photo uploaded successfully")
  browser.find_element_by_css_selector('.KingingBox__submit-btn').click()
else:
  print("Element not found")

I save into the inserted_photo variable the element which shows me that the photo uploaded successfully. Then if this variable is displayed this means that the photo uploaded properly. Thus, since we have the text and the photo we are ready to click on the 'Post' button.

Try to use static attributes that are not dynamic and there is no danger of future changes. This way you create stability to your code. Because choosing such an xpath like in your example, is risky. If a div or another tag is going to be excluded or included immediately the xpath is not useful.

PS: I uploaded two posts because of testing, so sorry I couldn't try it other way.

halfer
  • 19,824
  • 17
  • 99
  • 186
dpapadopoulos
  • 1,834
  • 5
  • 23
  • 34