How to upload files using selenium webdriver


package selenium_examples;


import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;


public class Sample {

public static void main(String[] args)
{
WebDriver driver = new FirefoxDriver();
driver.get("http://www.files.com/");
driver.manage().window().maximize();
WebElement uploadElement = driver.findElement(By.name("file_0"));
   driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
uploadElement.sendKeys("E:\\index.jpg");

 
}
}


 
upload file in selenium webdriver


How to scroll down the page through Selenium Web Driver

Page Scroll down script using selenium webdriver

package selenium_examples;


import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;


public class Sample {

public static void main(String[] args)
{

WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.co.in/preferences?hl=en#languages");
driver.findElement(By.linkText("Edit")).click();
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("scroll(0, 300)");

 

}
}


Page Scroll up script using selenium webdriver

package selenium_examples;


import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;


public class Sample {

public static void main(String[] args)
{

WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.co.in/preferences?hl=en#languages");
driver.findElement(By.linkText("Edit")).click();
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("scroll(300, 0)");
 

}
}



 



 

How to right click on a link and open link in new tab using selenium webdriver

Script1:-

package selenium_examples;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;

public class Sample {

public static void main(String[] args)
{

WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com");

WebElement glink = driver.findElement(By.linkText("About"));
                                Actions action= new Actions(driver);
action.contextClick(glink).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.RETURN).build().perform();


}
}


 
The above script clicks on 'About' link in google page and opens 'About google' page in new tab.

right click on link in webdriver


contextClick()-Performs a context-click at the current mouse location.

contextClick(WebElement onElement)-Performs a context-click at middle of the given element

Script2:-

Below script clicks on 'About' link in google page and opens 'About google' page in new window.

package selenium_examples;



import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;

public class Sample {

public static void main(String[] args)
{

WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com");

WebElement glink = driver.findElement(By.linkText("About"));
                Actions action= new Actions(driver);
action.contextClick(glink).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.RETURN).build().perform();


}
}


 

How to highlight an element using selenium webdriver

Script to highlight element border

Script1:-

package selenium_examples;


import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;


public class Sample {

public static void main(String[] args)
{

WebDriver driver = new FirefoxDriver();
       driver.get("http://www.seleniumhq.org/");
       driver.manage().window().maximize();
      WebElement element = driver.findElement(By.name("q"));
       JavascriptExecutor js = (JavascriptExecutor)driver;
       js.executeScript("arguments[0].style.border='5px solid blue'", element);
       }
}

Above script highlights search textbox border in www.seleniumhq.org website.

highlight element in selenium webdriver



Script2:-

package selenium_examples;


import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;


public class Sample {

public static void main(String[] args)
{

WebDriver driver = new FirefoxDriver();
       driver.get("https://www.google.co.in/");
       driver.manage().window().maximize();
      WebElement element = driver.findElement(By.name("btnK"));
       JavascriptExecutor js = (JavascriptExecutor)driver;
       js.executeScript("arguments[0].style.border='5px solid red'", element);
       }
}



 


Related Posts Plugin for WordPress, Blogger...

Fun and Knowledge