I want to write an automated JUnit test script with Selenium Web Driver to load a Wikipedia page and then change the title of the page loaded.

import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;
import org.junit.*;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;

public class wikiTest {
  private WebDriver driver;
  private String baseUrl;
  private boolean acceptNextAlert = true;
  private StringBuffer verificationErrors = new StringBuffer();

  public void setUp() throws Exception {
    driver = new FirefoxDriver();
    baseUrl = "https://www.wikipedia.org/";
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

  public void testwiki() throws Exception {
      WebDriverWait wait;
    driver.get(baseUrl + "/");
    driver.findElement(By.id("searchInput")).sendKeys("fish anatomy");
    wait = new WebDriverWait(driver, 10);

    /* in here I want to change the title of the page


  public void tearDown() throws Exception {
   // driver.quit();
    String verificationErrorString = verificationErrors.toString();
    if (!"".equals(verificationErrorString)) {

  private boolean isElementPresent(By by) {
    try {
      return true;
    } catch (NoSuchElementException e) {
      return false;

  private boolean isAlertPresent() {
    try {
      return true;
    } catch (NoAlertPresentException e) {
      return false;

  private String closeAlertAndGetItsText() {
    try {
      Alert alert = driver.switchTo().alert();
      String alertText = alert.getText();
      if (acceptNextAlert) {
      } else {
      return alertText;
    } finally {
      acceptNextAlert = true;

Any help is appreciated.


I'm not sure why you would ever want to do that, but one way is to simply execute some javascript.

import org.openqa.selenium.JavascriptExecutor;    

((JavascriptExecutor)driver).executeScript("document.title = 'new title'");


Changing text on a webpage (as per the comments) is achievable with pretty much the same method. just execute some javascript. so, inside the quotes instead of document.title = 'new title' use document.getElementById('firstHeading').innerHTML = 'new text' or for modern browsers use textContent instead of innerHTML.

  • But this is do nothing. Sep 3 '15 at 18:42
  • it works for me. do you have any kind of error or anything? Sep 3 '15 at 19:13
  • No errors was shown.It just load the page and then shows test is 100% passed . I just import the libraries and then enter the other line you mentioned in the position where I mentioned where it need to be execute. Sep 3 '15 at 19:25
  • Are you asking how to temporarily change the title shown in the tab at the top of the screen? Or are you asking how to edit a Wikipedia article and modify the articles title? I suspect you are really asking for the latter, whilst you appeared to be asking for the former :)
    – Ardesco
    Sep 4 '15 at 10:16
  • Yes.the solution given by @Xyclos do the former one.but is there any way to do the latter? Sep 7 '15 at 13:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.