I am trying the below code to test drag and drop in Selenium 3.0 and find that code is not working, meaning that it's not showing any error and also not providing expected result.
I have tried the same code in selenium 2.53 and it's working . Kindly someone review my code for the same and let me know if I missed something.
Selenium 3.0
Browser : Mozilla 2.52
package dynamicXpath;
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;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.interactions.Action;
import org.openqa.selenium.interactions.Actions;
public class refermeprobI {
public static void main(String[] args) throws InterruptedException{
System.setProperty("webdriver.gecko.driver", "D:\\Drivers\\geckodriver.exe");
FirefoxProfile profile = new FirefoxProfile();
profile.setEnableNativeEvents(true);
WebDriver driver = new FirefoxDriver(profile);
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
driver.get("https://the-internet.herokuapp.com/drag_and_drop");
Actions act = new Actions(driver);
WebElement src = driver.findElement(By.xpath("//*[#id='column-a']"));
WebElement dst = driver.findElement(By.xpath("//*[#id='column-b']"));
act.dragAndDrop(src, dst).build().perform();
System.out.println(driver.findElement(By.xpath("//*[#id='column-b']/header")).getText());
}
}
I've checked your code. Everything is fine except if you use Selenium 3.0.0 then need to set Desired Capabilities. I've also checked your code with Selenium latest 3.4. If you use Selenium 3.4 then you don't need to set Desired Capabilities. I used Firefox 52.
I hope that this info will help you to understand the problem you have encountered.
Thanks
You can also try following:
act.clickAndHold(src).moveToElement(dst).release(src).build().perform();
This works in certain scenarios where dragAndDrop() doesn't.
Related
I'm trying to play with the Selenium 4 features in eclipse with java but can't seem to get them to work, which i'm assuming must be my mistake in configuration, so I'd appreciate if anyone can explain where i'm going wrong and how I can correct it?
Code below: I get an error warning on the last line (containing the newWindow() method) and the error message is as per the Title of this post.
I've downloaded the selenium-java-4.0.0-alpha-4 from here https://selenium-release.storage.googleapis.com/index.html?path=4.0-alpha4/
i've created a new eclipse java project, and unzipped and included all of the jar files in my java build path, but it doesn't seem to recognise / or be able to find the newWindow() method
package practice;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.WindowType;
import org.openqa.selenium.chrome.ChromeDriver;
import static org.openqa.selenium.support.locators.RelativeLocator.withTagName;
import org.openqa.selenium.By;
public class Sel4alpha4 {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver","C:\\Users\\me\\Downloads\\chromedriver_win32\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://www.google.com");
driver.switchTo().newWindow(WindowType.TAB); // this line has the error
}
}
There might be two problems:
1) Incorrect Import- You should import the following:
org.openqa.selenium.WindowType;
2) Issue with the version you are using. Try to upgrade the selenium version.
Version:
selenium 4.0.0-beta-4
Please try to download the updated jar from the below link:
https://www.selenium.dev/downloads/
or
https://selenium-release.storage.googleapis.com/index.html?path=4.0-beta-4/
I started to learn SeleniumWeb driver with Java and I write some code like this :
package firstPackage;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class firstScript {
public static void main(String[] args) {
// declaration and instantiation of objects/variables
System.setProperty("webdriver.firefox.marionette","C:\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("http//:www.google.com");
WebElement element = driver.findElement(By.name("q"));
element.sendKeys("mysql excel 2013");
element.submit();
}
}
If I run this code, mozilla only start, It is not contiune. I want to it to "google" and search "mysql excell 2013". How can I do?
I am using the selenium-server-standalone-3.5.3.jar in which this code is not working but when I changed jar from 3.5.3 to 2.44.0 then Its working fine.
Its opening the firefox and search the "mysql excel 2013" and got the results of it.
So you need to change the selenium version or need to change the browser from firefox to chrome.
I am trying to automate an android application,
I have taken following code,
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.android.AndroidDriver;
public class LaunchElGiftoAndroid {
public static void main(String args[])throws Exception
{
AndroidDriver ad=new AndroidDriver();
System.out.println("Started");
ad.get("http://www.gmail.com");
System.out.println("Application Title"+ ad.getTitle());
Thread.sleep(2000);
ad.findElement(By.name("Email")).sendKeys("testing");
ad.findElement(By.name("Passwd")).sendKeys("type password");
ad.findElement(By.name("signIn")).click();
System.out.println("Opened");
ad.close();
}
}
I have installed the Web driver apk properly.
i was getting problem with the following import statement.
import org.openqa.selenium.android.AndroidDriver;
I believe you using the old AndroidDriver.
You should be using Selendroid in that case.
http://selendroid.io/mobileWeb.html
If you check this webpage. Selenium recommends you to switch to Selendroid as it has more features and options. You can even do automation inside an app!
I have prepared the environment for test automation of Android Application using eclipse. I have followed the instruction from the below site:
https://code.google.com/p/selenium/wiki/AndroidDriver#Setup_the_Environment
I have copied the following code from the above website as below:
import junit.framework.TestCase;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.android.AndroidDriver;
public class OneTest extends TestCase {
public void testGoogle() throws Exception {
WebDriver driver = new AndroidDriver();
// And now use this to visit Google
driver.get("http://www.google.com");
// Find the text input element by its name
WebElement element = driver.findElement(By.name("q"));
// Enter something to search for
element.sendKeys("Cheese!");
// Now submit the form. WebDriver will find the form for us from the element
element.submit();
// Check the title of the page
System.out.println("Page title is: " + driver.getTitle());
driver.quit();
}
}
But error as "WebDriver cannot be resolved to a type" was found at the following line:
WebDriver driver = new AndroidDriver();
Note: I have added "selenium-server-standalone-2.33.0.jar" to Java Build Path
Only one import statement is needed to fix the error. import the following and that's it:
import org.openqa.selenium.WebDriver;
You need to properly install the android server available here http://code.google.com/p/selenium/downloads/list .
Follow this tutorial http://code.google.com/p/selenium/wiki/AndroidDriver#Install_the_Android_SDK
regarding how to install the android web driver.
Add - import org.openqa.selenium.WebElement;
I am doing project in Maven. I try to get pages from URl. Till now I am successful in getting pages from web. But I have two questions,
Qustions,
Below code takes around 14 seconds to get any two URL pages, how can I reduce this time, Help me in optimizing this.
After completing the execution, it does not exits from code. Why ?
I ended the code with driver.close(). Then, why, it does not exits successfully. I added snapshots before starting and after completing the process. Please see these.
Help me in my problem. Please.
My code:-
package XXX.YYY.ZZZ.Template_Matching;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import java.io.IOException;
import org.junit.Assert;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriverService;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.w3c.dom.Document;
public class HtmlUnit {
public static void main(String[] args) throws Exception {
String url1 = "http://www.jabong.com/men/shoes/men-loafers/?source=home-leftnav";
String url2 = "http://www.jabong.com/fastrack-9915Pp36J-Black-Pink-Analog-Watch-198499.html";
DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, "C://Users//jhamb//Desktop//phantomjs-1.9.0-windows//phantomjs.exe");
WebDriver driver = new PhantomJSDriver(caps);
driver.get(url1);
String hml1 = driver.getPageSource();
driver.get(url2);
String hml2 = driver.getPageSource();
driver.close();
//System.out.println(hml1);
//System.out.println(hml2);
Document doc1 = Jsoup.parse(hml1);
Document doc2 = Jsoup.parse(hml2);
// Some operations using these DOM tree, just like , comparing Templates of two URLS
}
}
Snapshot before starting the process,
Snapshot after completing the process, when it waits for no reason,
You need to use
driver.quit();
instead of
driver.close();
I suspect the driver is creating a thread and it did not exit. Try adding a System.exit at the end of main and see whether it solves your issue.