Site Search:

How to automatically update blogger page with selenium driver

<Back

You can use selenium driver to automatically update your blogger page.
Step 1. create a new default maven project




Step 2. change the default maven project's pom.xml, add the selenium dependencies. 
The maven pom.xml is the following:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>blogger</groupId>
  <artifactId>bloger-updater</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>bloger-updater</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>2.53.0</version>
    </dependency>
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-chrome-driver</artifactId>
        <version>2.53.0</version>
    </dependency>
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>htmlunit-driver</artifactId>
        <version>2.21</version>
    </dependency>
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-server</artifactId>
        <version>2.53.0</version>
    </dependency> 
  </dependencies>
</project>

Step 3. drop the following code into your java folder, replace those Change_ME.
The automation code is the following.
Basically, you login google account, then edit your post then submit. Selenium driver will drive a local firefox browser to do these automatically.
package blogger.bloger_updater;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;

public class BloggerTestPageUpdater  {
    public static void main(String[] args) {
        // Create a new instance of the Firefox driver
        // Notice that the remainder of the code relies on the interface, 
        // not the implementation.
        WebDriver driver = new FirefoxDriver();
        //System.setProperty("webdriver.chrome.driver", "/Users/homenetwork/Downloads/chromedriver");
        //WebDriver driver = new ChromeDriver();
        
        //Launch the Online Store Website
        driver.get("http://www.gmail.com");

        // Print a Log In message to the screen
        System.out.println("Successfully opened the website gmail.com");

        driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
        //Maximize window
        driver.manage().window().maximize();
        
        //Enter Username
        driver.findElement(By.id("Email")).sendKeys("Change_ME");
        //Click on Next
        driver.findElement(By.id("next")).click();
        // Wait For Page To Load
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        //Enter Password
        driver.findElement(By.id("Passwd")).sendKeys("Change_ME");
        
        // Click on 'Sign In' button
        driver.findElement(By.id("signIn")).click();
        
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.navigate().to("https://www.blogger.com/blogger.g?blogID=Change_ME#editor/target=post;postID=Change_ME");
        
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        //WebElement element = driver.findElement(By.id("postingComposeBox"));
        WebElement iframe = driver.findElement(By.id("postingComposeBox"));
        driver.switchTo().frame(iframe);

        WebElement description = driver.findElement(By.cssSelector("body"));
        ((JavascriptExecutor)driver).executeScript("arguments[0].innerHTML = '<p>from selenium driver</p>'", description);
        driver.switchTo().defaultContent();
        
        driver.findElement(By.xpath("//button[contains(.,'Update')]")).click();
        
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        
        driver.navigate().to("http://xyzcode.blogspot.com/2016/04/test-page.html");

        //Close the browser
        //driver.quit();
    }
}

step 4. right click the project, select run as maven install


step 5. right click BloggerTestPageUpdater.java, select run as Java Application

The following page then get updated

1 comment:

  1. Let’s look at the codes to refresh the Browser in many different ways with Selenium WebDriver. Sometimes on certain browsers or on certain pages, normal refresh command deos not work, in those situations we can easily usethese commands to solve out purpose.
    Best selenium training institute in chennai

    ReplyDelete