I am trying double click and copy paste scenarios on yahoo.com but it is not working. Want to copy the text "Sign in" and paste in username
{ package com.yahoo.com;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;`
import org.openqa.selenium.Keys;
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.interactions.Actions;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class YahooTests {
WebDriver driver;
#Test
public void test01_InvokeBrowserApp(){
System.setProperty("webdriver.chrome.driver", "C:\\SeleniumDrivers\\chromedriver_win32\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.manage().window().maximize();
}
{ Actions act = new Actions(driver);
WebElement copy = driver.findElement(By.xpath(".//*[#id='mbr-login-greeting']"));
act.moveToElement(copy).doubleClick().sendKeys(Keys.CONTROL+"C").build().perform();
act.doubleClick(paste).sendKeys(Keys.CONTROL+"V");
WebElement paste = driver.findElement(By.xpath(".//*[#id='login-username']"));
}
Try This hope problem will be solved
act.moveToElement(By.xpath(".//*[#id='mbr-login-greeting']")).doubleClick().build().perform();
// catch here is double click on the text will by default select the text
// now apply copy command
driver.findElement(By.xpath(".//*[#id='mbr-login-greeting']")).sendKeys(Keys.chord(Keys.CONTROL,"c"));
// now apply the command to paste
driver.findElement (By.xpath(".//*[#id='login-username']").sendKeys(Keys.chord(Keys.CONTROL, "v"));
Related
I am practicing against the following website https://www.easyjet.com/en
I am passing a value of "London" into the Origin search box. This returns six airport matches. I'm then trying to trawl through the results and select the one that contains the word "Luton".
My code so far is:
package d_practise;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class easyjetMenu {
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "C:\\Work\\Drivers\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://www.easyjet.com/");
Thread.sleep(2000);
WebElement d = driver.findElement(By.cssSelector("input[name='origin']"));
d.click();
d.sendKeys("London");
while(!d.getText().contains("Luton")) {
d.sendKeys(Keys.DOWN);
}
if(d.getText().contains("Luton")) {
d.sendKeys(Keys.ENTER);
}
}
}
This just continuously loops and no match is found. I have tried various phrases but no joy.
Anyone please able to help?
On passing the value of London into the Origin search box, to click on the autocomplete/autosuggestion with text as Luton you have to induce WebDriverWait for the visibilityOfAllElementsLocatedBy() and then click() on the desired element using the following Locator Strategy:
Code Block:
import java.util.Collections;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class easyjet_com_origin {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver","C:\\WebDrivers\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("--start-maximized");
options.setExperimentalOption("excludeSwitches", Collections.singletonList("enable-automation"));
options.setExperimentalOption("useAutomationExtension", false);
WebDriver driver = new ChromeDriver(options);
driver.get("https://www.easyjet.com/en/");
WebElement d = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("input[name='origin']")));
d.click();
d.sendKeys("London");
//List<WebElement> origins = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath("//ul[#id='ui-id-1']//li/a/span")));
List<WebElement> origins = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.cssSelector("ul#ui-id-1 li>a>span")));
for(WebElement origin:origins)
{
if(origin.getText().contains("Luton"))
origin.click();
}
}
}
Browser Snapshot:
I have 100+ button in the page and I have to click on each button and to verify the link and page which opens after clicking.
so how to click without writing xpath for all 100+ button, i just need to click them and for each button different page will open.
Pls help me to proceed my selenium test
After you click the first button a new DOM is loaded, so click on the second (and third ...) will end with StaleElementReferenceException. You need to get all the href values first and then try them one by one. Just tried on web bbc.com with this code:
package navi;
import java.util.ArrayList;
import java.util.List;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.firefox.internal.ProfilesIni;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class Himanshu {
public static WebDriver driver;
#BeforeClass
public static void setUpClass() {
FirefoxOptions options = new FirefoxOptions();
ProfilesIni allProfiles = new ProfilesIni();
FirefoxProfile selenium_profile = allProfiles.getProfile("selenium_profile");
options.setProfile(selenium_profile);
options.setBinary("C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe");
System.setProperty("webdriver.gecko.driver", "C:\\Users\\pburgr\\Desktop\\geckodriver-v0.20.0-win64\\geckodriver.exe");
driver = new FirefoxDriver(options);
driver.manage().window().maximize();
}
#Before public void setUp() {} #After public void tearDown() {}
#AfterClass public static void tearDownClass() {driver.quit();}
#Test
public void lots_of_buttons() throws InterruptedException {
driver.get("http://www.bbc.com/");
// wait to page is fully loaded
wait_sec(driver, 5).until(ExpectedConditions.elementToBeClickable(By.xpath("//*[#id=\"orb-header\"]/div[1]/div[1]/a/img")));
// get all the desired webelements
List<WebElement> header_links = driver.findElements(By.xpath("//a[contains(#href,'x')]"));
// create new arraylist
ArrayList<String> hrefs = new ArrayList<String>();
// collect all the href values
for (WebElement link: header_links) {
hrefs.add(link.getAttribute("href"));
}
// visit all the URLs
for (String url: hrefs) {
driver.get(url);
Thread.sleep(1000);
// do some stuff here
}
}
// modified wait method
public WebDriverWait wait_sec(WebDriver driver, int sec) {
return new WebDriverWait(driver, sec);
}
}
Im using automation with Selenium and JAVA (Eclipse) and im trying to aplying an automation test but i cant select the "prompt box" for inssert the text into the fields...
my code dont send "text" to my fields
this is a windows like a pop up where i need insert text and clic on "Aceptar", but actually my code doesn't work, just open the navigator and clic in the button and the test finished..
this is my code:
package Driver;
import java.util.regex.Pattern;
import javax.swing.JOptionPane;
import java.util.concurrent.TimeUnit;
import org.testng.Assert;
import org.testng.annotations.*;
import com.thoughtworks.selenium.webdriven.commands.SelectWindow;
import static org.testng.Assert.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.server.handler.GetAllWindowHandles;
import org.openqa.selenium.support.ui.Select;
import java.io.*;
public class Parametria {
public static void main (String[] args) throws Exception{
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get("MY WWWW URL");
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.findElement(By.id("form:table:buttonAdd")).click();
driver.switchTo().frame("form:panelAdd_content");
driver.findElement(By.id("form:headerType")).clear();
driver.findElement(By.xpath("//*[#id='form:headerType']']")).sendKeys("Parametro_1");
// driver.findElement(By.name("form:headerType']']")).sendKeys("Parametro_1");
driver.findElement(By.id("form:nameType")).clear();
driver.findElement(By.xpath("//*[#id='form:nameType']")).sendKeys("Parametro_2");
driver.findElement(By.xpath("//*[#id='form:value1']/span")).click();
driver.findElement(By.xpath("//*[#id='form:j_idt72']")).click();
driver.navigate().back();
driver.quit();
}
}
How to test or verify check box is selected or not selected by using selenium webdriver java
package newpackage;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.WebElement;
//import org.openqa.selenium.firefox.*;
import java.util.concurrent.*;
public class Qemr { public static void main(String[]args){
System.setProperty("webdriver.gecko.driver","D:\\geckodriver-v0.10.0-win64\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
String baseUrl = "http://192.168.1.3:9091";
driver.get(baseUrl);
WebElement chkPersist = driver.findElement(By.name("remember"));
chkPersist=click();
for(int i=0;i<2;i++){
System.out.println("chkPersist.isChecked()");
}
}
}
yes there are methods available which you can use for ny kind of controls, for eg. for check box you can use isSelected() method which returns boolean value i.e. 0/1
for your scenario
package newpackage;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.WebElement;
//import org.openqa.selenium.firefox.*;
import java.util.concurrent.*;
public class Qemr {
public static void main(String[]args)
{
System.setProperty("webdriver.gecko.driver","D:\\geckodriver-v0.10.0-win64\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
String baseUrl = "http://192.168.1.3:9091";
driver.get(baseUrl);
WebElement chkPersist = driver.findElement(By.name("remember"));
//chkPersist.click();
if(chkPersist.isSelected())
{
System.out.println("Check box is Selected..");
}
}
}
driver.findElement("").isSelected() option is there to check whether Checkbox is selected. you can find solution in your updated code:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.WebElement;
//import org.openqa.selenium.firefox.*;
import java.util.concurrent.*;
public class StackOverFlow1
{
public static void main(String[]args)
{
System.setProperty("webdriver.gecko.driver","D:\\geckodriver-v0.10.0-win64\\geckodriver.exe");
WebDriver driver=new FirefoxDriver();
String baseUrl="http://192.168.1.3:9091";
driver.get(baseUrl);
boolean chkPersist=driver.findElement(By.name("remember")).isSelected();
if(chkPersist)
{
System.out.println("chkPersist is in selected state");
}
}
}
}
Question : How to test or verify check box is selected or not selected by using selenium webdriver java.
Answer : Yes we can verify webelement is selected or not using isSelected() method
package newpackage;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.WebElement;
import java.util.concurrent.*;
public class Qemr {
public static void main(String[] args) {
System.setProperty("webdriver.gecko.driver", "D:\\geckodriver-v0.10.0-win64\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
String baseUrl = "http://192.168.1.3:9091";
driver.get(baseUrl);
WebElement chkPersist = driver.findElement(By.name("remember"));
// Verify chkPersist element is Selected or Not ?
if (chkPersist.isSelected()) {
// if chkPersist element is selected then we print message
System.out.println("chkPersist element is already selected");
} else {
// if chkPersist element is not selected then we select / click on chkPersist element
chkPersist.click();
}
}
}
The testpage I am working on is http://www.quackit.com/html/templates/frames/frames_example_1.html in firefox.
In it I have to click on Code Generator WebElement and take a screenshot of the opened page with Selenium Webdriver, JAVA.
Whatever method I tried, Selenium is giving exception as
Unable to locate element.
It has target as _blank.
Please find the code below
import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Keys;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.Point;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class Quackit {
public static void main(String[] args) throws Exception {
WebDriver driver = new FirefoxDriver();
driver.get("http://www.quackit.com/html/templates/frames/frames_example_1.html");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
String parentWindow = driver.getWindowHandle();
WebElement e = driver.findElement(By.linkText("Code Generator "));
// ensure that link always opens in the current window
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].setAttribute('target', arguments[1]);", e, "_self");
e.click();
for (String winHandle : driver.getWindowHandles()) {
driver.switchTo().window(winHandle); // switch focus of WebDriver to the next found window handle (that's your newly opened window)
}
TakesScreenshot ts = (TakesScreenshot)driver;
File source = ts.getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(source,new File ("./Screenshots/codegenerator.png"));
}
}
This works:
driver.switchTo().frame("content");
WebElement e = driver.findElement(By.xpath("//a[contains(.,'Code Generator')]"));
e.click();
You have to switch to the frame first and should use contains() to locate the element.
The issue is that Code Generator link is inside the frame/iframe. so if elements are inside the frame, first we need to switch into the frame then only we can find elements that frame.
Once work or actions inside frame is completed then need to switch on to default content.
WebDriver driver = new FirefoxDriver();
driver.get("http://www.quackit.com/html/templates/frames/frames_example_1.html");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
driver.switchTo().frame("content");
WebElement e = driver.findElement(By.linkText("Code Generator"));
e.click();
driver.switchTo().defaultContent();
//try for window handles here
Thank You,
Murali