I want to perform drag and drop in W3school web page using selenium. Code is working fine but output is not showing on the webpage.
link is :- http://www.w3schools.com/html/tryit.asp?filename=tryhtml5_draganddrop
My code is :-
public String dragAndDrop(String object,String data){
APP_LOGS.debug("waiting for popup closer");
try{
driver.switchTo().frame("iframeResult");
WebElement element = driver.findElement(By.xpath(".//*[#id='drag1']"));
WebElement target = driver.findElement(By.xpath(".//*[#id='div1']"));
(new Actions(driver)).dragAndDrop(element, target).build().perform();
}catch(Exception e){
return Constants.KEYWORD_FAIL+" -- Unable to drag"+e.getMessage();
}
return Constants.KEYWORD_PASS;
}
We can also interact with keyboard / mouse events using Actions class and robot class in Selenium. I have used Robot class to resolve your issue.
The Robot class is present in java.awt package. You can check all the methods in the docs.
public static void Task1() throws AWTError, AWTException, InterruptedException
{
WebDriver driver = new FirefoxDriver();
driver.get("https://www.w3schools.com/html/tryit.asp?filename=tryhtml5_draganddrop");
driver.switchTo().frame("iframeResult");
WebElement element1 = driver.findElement(By.xpath(".//img[#id='drag1']"));
WebElement element2 = driver.findElement(By.xpath(".//*[#id='div1']"));
Actions action = new Actions(driver);
Point element3 = driver.findElement(By.xpath(".//*[#id='drag1']")).getLocation();
int i=element3.getX()+800;
int b=element3.getY()+250;
Robot robot = new Robot();
robot.mouseMove(i, b);
// Press left click of mouse
robot.mousePress( InputEvent.BUTTON1_DOWN_MASK);
robot.delay(4000);
robot.mouseMove(i+20, b-120);
robot.mousePress( InputEvent.BUTTON1_DOWN_MASK);
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
Thread.sleep(10000);
driver.close();
}
Related
Drop Down Buttons
I want to click on "Entire Order(Same Local Site)". After clicking on OPTIONS, menu gets displayed and after hoavering over "Copy Order" sub-menu gets displayed.
public void moveToTask(WebElement ele1)
{
Actions ac = new Actions(driver);
log.info("Task Link is: "+ele1);
ac.moveToElement(ele1);
log.info("Moved to Element by action using class");
}
public void MoveToTaskandClick(WebElement ele)
{
Actions ac = new Actions(driver);
ac.moveToElement(ele).perform();
ac.click().build().perform();
log.info("Move to task and clicked on"+ele);
}
public void copyOrder() throws InterruptedException
{
OPTIONS.click();
Thread.sleep(3000);
dropdownhelper.moveToTask(CopyOrder);
Thread.sleep(5000);
dropdownhelper.MoveToTaskandClick(SameSite);
Thread.sleep(5000);
alerthelper.acceptAlertIfPresent();
}
It neither throws error nor it clicks on same site. It clicks only on OPTIONS and then nothing happens.
XPath I am using:
#FindBy(xpath = "//img[#name='order_options_btn']")
public WebElement OPTIONS;
#FindBy(xpath="//div[#id='copy_menu']")
public WebElement CopyOrder;
#FindBy(xpath="//div[#id='copy_menu']/table/tbody/tr[4]/td[2]/nobr")
public WebElement SameSite;
XML Code
Try to use this approach:
OPTIONS.click();
Actions action = new Actions(driver);
action.moveToElement(CopyOrder).perform(); // element you want to hover
driver.(EntireOrder).click();
I am trying to learn Selenium from Youtube. I have written the simple code below on Frames. I want to click on linkText which is not visible but manually can scroll and click on it. I am trying with the below code but getting the error:
org.openqa.selenium.WebDriverException: unknown error: Element is not clickable
My code:
public class Frame_Test {
WebDriver driver;
#Test
public void test1() {
System.setProperty("webdriver.chrome.driver", "C:\\chromedriver_win32\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("--start-maximized");
driver = new ChromeDriver(options);
driver.get("http://seleniumhq.github.io/selenium/docs/api/java/index.html");
driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
driver.switchTo().frame("packageListFrame");
driver.findElement(By.linkText("org.openqa.selenium.safari")).click();
}
}
You can scroll down with screen height using this code:
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("window.scrollTo(0, document.body.scrollHeight);");
Scrolling down for some number of pixels:
js.executeScript("scroll(0, 300);");
Scrolling up for some number of pixels:
js.executeScript("scroll(0, -300);");
Hope it helps you!
You can use the below method:
public static void scrollToElement(By elementToken, WebDriver driver){
WebElement element = driver.findElement(elementToken);
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", element);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
Or if you don't want to use the explicit method you can use the scroll code in your code:
driver.get("http://seleniumhq.github.io/selenium/docs/api/java/index.html";
driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
driver.switchTo().frame("packageListFrame");
WebElement element = driver.findElement(By.linkText("org.openqa.selenium.safari"));((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", element);
driver.findElement(By.linkText("org.openqa.selenium.safari")).click();
I am trying to test an E-Commerce website using Selenium webdriver. The problem in the test is that whenever I try to add stuff in the cart it just pops a news letter window which I tried handling using alert but I cannot.
Can someone please help me. I am attaching a screenshot below along with the code.
public class Ui {
public static void main(String[] args) {
System.setProperty("webdriver.gecko.driver","C:/New folder/geckodriver.exe");
//First Iam going to initialize the webdriver by using Firefox driver//
WebDriver driver = new FirefoxDriver();
driver.get("https://www.build.com/");
driver.manage().window().maximize();
driver.findElement(By.xpath(".//*[#id='search_txt']")).sendKeys("K-6626-6U ");
Actions enter = new Actions(driver);
enter.moveToElement(driver.findElement(By.xpath(".//*[#id='site-search']/div/button"))).click().build().perform();
}
}
First thing is - Its not an alert it a window popup So you need to locate the close button and then click
Use below code for the same :
public class Ui
{
public static void main(String[] args)
{
System.setProperty("webdriver.gecko.driver","C:/New folder/geckodriver.exe");
//First Iam going to initialize the webdriver by using Firefox driver//
WebDriver driver = new FirefoxDriver();
driver.get("https://www.build.com/");
driver.manage().window().maximize();
new WebDriverWait(driver, 60).until(ExpectedConditions.visibilityOf( driver.findElement(By.xpath("//button[#class='close external-close']")))).click();
}
}
Here you have to use ExplicitWait until popup get visible and then have to perform click. If you won't use the wait then it will throw ElementNotVisibleException.
wait for some time and click on escape
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.get("https://www.build.com/");
driver.manage().window().maximize();
//give own time
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
Actions enter = new Actions(driver);
enter.sendKeys(Keys.ESCAPE).perform();
}
I am using www.flipkart.com,where I want to hover over "Appliances" and click on "Television".
public static void main(String args[]) throws InterruptedException {
System.setProperty("webdriver.chrome.driver","C:\drivers\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://www.flipkart.com");
driver.manage().window().maximize();
Thread.sleep(1000);
WebElement mainMenu = driver.findElement(By.xpath("//a[#title='Appliances']"));
WebElement submenuxpath = driver.findElement(By.xpath("//li[#class='Wbt_B2'][2]//li[#class='_1KCOnI _1h5QLb']//a[#title='Televisions']"));
Actions builder = new Actions(driver);
builder.moveToElement(mainMenu).perform();
//builder.moveToElement(submenuxpath).click().perform();
//driver.click(submenuxpath);
Thread.sleep(1000);
driver.close();
}
It is able to hover over on "Appliances". But I am getting error in driver.click(submenupath) or builder.moveToElement(submenuxpath).click().perform(). Where I am going wrong?
driver.click(submenupath) error: The method click(WebElement) is undefined for the type WebDriver. Quick fix : add cast to driver. Even if I am doing add cast,it is not working then. For builder.moveToElement(submenuxpath).click().perform(),no error is coming,but it is not clicking also.
I am new to Selenium and trying to use Actions class to mouseover on the Profile icon available on linked in site to open the menu that appears on Mouseover of profile image.
Below is my code and when it reaches on to those lines the error comes : Unable to locate element..
This is happening with all the icons available on Linked on top bar ( messages / Flag icon etc.
Code :
public class LinkedIn {
WebDriver driver = new FirefoxDriver();
#BeforeTest
public void setUp() throws Exception {
String baseUrl = "http://www.linkedin.com/";
driver.get(baseUrl);
}
#Test
public void login() throws InterruptedException
{
WebElement login = driver.findElement(By.id("login-email"));
login.sendKeys("*****#gmail.com");
WebElement pwd = driver.findElement(By.id("login-password"));
pwd.sendKeys("*****");
WebElement in = driver.findElement(By.name("submit"));
in.click();
Thread.sleep(10000);
}
#Test
public void profile() {
// here it gives error to me : Unable to locate element
Actions action = new Actions(driver);
WebElement profile = driver.findElement(By.xpath("//*[#id='img-defer-id-1-25469']"));
action.moveToElement(profile).build().perform();
driver.quit();
}
}
It seems you have used incorrect xpath , Kindly check below example to mouse hover on Message button :
Thread.sleep(5000);
Actions action = new Actions(driver);
WebElement profile = driver.findElement(By.xpath("//*[#id='account-nav']/ul/li[1]"));
action.moveToElement(profile).build().perform();
Correct Xpaths are :
For Message Icon : "//*[#id='account-nav']/ul/li[1]"
For Connection Icon : //*[#id='dropdowntest']
Above code I just tested and working fine so will work for you.