DEV Community

Divita Goswami
Divita Goswami

Posted on

Automate tweeting using selenium

I have joined a challenge in twitter called the #100DaysOfCode challenge. It's a exciting challenge that keeps you motivated to learn and code. Link: https://www.100daysofcode.com/ .

About the challenge

The challenge is to code or do code related tasks till 100 days and tweet about each day's progress using the hash tag 100DaysOfCode.

https://twitter.com/hashtag/100DaysOfCode?src=hashtag_click

Birth of an idea

In this challenge i have a repetitive task of logging in and typing the same hash tag, i was learning selenium when it suddenly clicked, why not use the knowledge for automating my tweets!

Task steps that i have automated:
1. Logging on to twitter by giving credentials.
2. Typing the required # tags automatically, Typing my tweet
is manual for now.
3. Click on tweet.

A brief about the tool - Selenium

Selenium is the tool that i used to automate my task, so here is something about the tool and packages that I've used. It's a vast topic but here i have mentioned some important areas and explained it with my code.

Selenium has following two packages

  1. WebDriver
  2. WebElement

WebDriver : It is Open source API collection used to automate web applications.It supports different browsers. Has got method to load a web application on browsers.

WebElement : It is an interface, anything on a web page is called a web element eg: input box,textbox, buttons etc. we identify and operate on web elements using the object and methods of WebElement.

code block

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

The above code imports our packages - drivers, we need chrome driver to launch our app in the browser

public class autoTweet {
  WebDriver driver;
    System.setProperty("webdriver.chrome.driver","C:\\Users\\xyz\\chromedriver.exe"); // setting our driver path
      driver = new ChromeDriver();
      driver.get("https://twitter.com/home");// using driver loads twitter

Above code snippet is just launching twitter on chrome.

public void f() {
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

WebElement username = driver.findElement(By.xpath("/html/body/div/div/div/div[2]/main/div/div/div[1]/form/div/div[1]/label/div/div[2]/div/input"));
    username.sendKeys("xya@abc.com");// Give valid mail id, this is example

WebElement password = driver.findElement(By.xpath("/html/body/div/div/div/div[2]/main/div/div/div[1]/form/div/div[2]/label/div/div[2]/div/input"));
    password.sendKeys("*****"); // give your password
    driver.findElement(By.xpath("/html/body/div/div/div/div[2]/main/div/div/div[1]/form/div/div[3]/divSdiv")).click();// find Log in button and click

I have used time out because sometimes the script tries to find the element even before the page is loaded. So we wait for page to load all the elements.
Then i identify using locator xpath, the Username and password textbox and pass values using sendKeys() method.

(more about xpaths here : https://www.guru99.com/xpath-selenium.html).

( About sendKeys here : https://www.guru99.com/accessing-forms-in-webdriver.html )

WebElement tweet = driver.findElement(By.xpath("/html/body/div/div/div/div[2]/main/div/div/div/div/div/div[2]/div/div[2]/div[1]/div/div/div/div[2]/div[1]/div/div/div/div/div/div/div/div/div/div[1]/div/div/div/div[2]/div/div/div/div")); // find the tweeting text box

    tweet.sendKeys("#100DaysOfCode #codeNewbies"); //type auto #100DaysOfCode #codeNewbies
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

Lastly i identify the web element text box for tweeting and pass my #100DaysOfCode #codeNewbies
There is a lot of scope for improvement and this particular twitter case can be extended further, i have the idea but need to work on it.

Future Scope

Fetching my log information from git hub and putting it directly to my tweet, as in this case i mostly tweet the same update as in git hub logs.

This is my first technical post on dev.to Please help me improve.
I welcome all feedbacks, constructive criticism.
Feel Free to give Feedbacks

Thanks!

Top comments (0)