Unable to Drag and Drop for web driver in Firefox - java

Below is the code i tried
WebDriver driver = new FirefoxDriver();
driver.get("http://www.w3schools.com/html/html5_draganddrop.asp");
Actions builder = new Actions(driver);
WebElement src = driver.findElement(By.id("drag1"));
WebElement des = driver.findElement(By.id("div2"));
builder.clickAndHold(src).build().perform();
builder.moveToElement(des).build().perform();
builder.release(des).build().perform();
driver.manage().timeouts().implicitlyWait(3,TimeUnit.MINUTES);
I don't see the drag and drop operation happening, Although no error is returned in the console.
Am I missing any step here ?

Please give a try with following:
builder.dragAndDrop(src, des).build().perform();
If above doesn't help you, see if following helps:
http://elementalselenium.com/tips/39-drag-and-drop

Related

Selenium's IE driver doesn't load page?

I try to load webpage and extract some docs, but after typing in code and pressing "search" button, nothing shows up. When i do it on my regular website, it works fine. Should I somehow set my WebDriver differently?
I tried with simple firefox driver but it also failed.
WebDriver driver = new InternetExplorerDriver();
driver.get("https://ekrs.ms.gov.pl/rdf/pd/");
WebElement krsinput = driver.findElement(By.name("unloggedForm:krs2"));
krsinput.sendKeys(String.valueOf(myNumber);
WebElement szukajbutton = driver.findElement(By.name("unloggedForm:submit"));
szukajbutton.click();
I typed in "331515" in the inputbox (there's only one) and after pressing "szukaj", there should be table showing below button.
First, there is a tip on input field: Pole KRS jest dziesięcioznakowe.
So, your code should look like
String strMyNumber = "0000331515";
WebDriver driver = new InternetExplorerDriver();
driver.get("https://ekrs.ms.gov.pl/rdf/pd/");
WebElement krsinput = driver.findElement(By.name("unloggedForm:krs2"));
krsinput.sendKeys(strMyNumber);
WebElement szukajbutton = driver.findElement(By.name("unloggedForm:submit"));
szukajbutton.click();
And your code is fully workable.
By my opinion, it's a website's issue. I have tried few correct format case numbers that I've found on some Polish websites, all of them gave no result to me.

How to clear the default values in the username textfield and enter my value in selenium webdriver

I have tried the following code but it is throwing the exception (ElementNotVisibleException)
FirefoxDriver dr = new FirefoxDriver();
dr.get("http://54.169.235.143/book.html?v=0.03");
System.out.println("First Testcase");
System.out.println(dr.findElement(By.id("user_name")));
dr.findElement(By.id("user_name"));
dr.findElement(By.id("user_name")).click();
dr.findElement(By.id("user_name")).getAttribute("user_name");
dr.findElement(By.id("user_name")).clear();
dr.findElement(By.id("user_name")).sendKeys("student100");
What am I doing wrong and how to fix it?
Actually your page taking time to load so web driver need wait until element gets visible , Below code will solve your issue :
WebDriverWait wait= new WebDriverWait(dr,30);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("user_name")));
dr.findElement(By.id("user_name")).clear();
dr.findElement(By.id("user_name")).sendKeys("test");
wait= new WebDriverWait(dr,30);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("pass_word")));
dr.findElement(By.id("pass_word")).clear();
dr.findElement(By.id("pass_word")).sendKeys("test");
I have just added wait for elements.
n software testing services this can be achieved by many ways some of the options are displayed above remaining are as follow.
Using java script
driver.executeScript("document.getElementByXpath('element').setAttribute('value', 'abc')");
Using action class Actions actions = new Actions(driver);
actions.click(driver.findElement(element) .keyDown(Keys.CONTROL).sendKeys("a").keyUp(Keys.CONTROL).sendKeys(Keys.BACK_SPACE).build().perform());

Mouse clickAndHold() not working properly on Firefox Using Selenium Webdriver

I am using selenium webdriver client 2.39 and Firefox 26.
Mouse click and hold event does not work properly. My code is like
WebDriver driver=new FirefoxDriver();
driver.get("http://startingwithseleniumwebdriver.blogspot.com/2013/12/frmset1.html");
WebElement multiSelectDropDown=driver.findElement(By.name("multiselectdropdown"));
List<WebElement> dropdownlists = multiSelectDropDown.findElements(By.tagName("option"));
Actions builder=new Actions(driver);
builder.clickAndHold(dropdownlists.get(0)).
clickAndHold(dropdownlists.get(6)).click().build();
This code does not give any error but select only one element.
I can bypass this issue using other way but I want to know whay it is not working.
I face the same problem but it select the element from start to last and give some Error like
Cannot perform native interaction: Could not get node for element - cannot interact
I got the solution by this way you can do this for your problem
builder.clickAndHold(dropdownlists.get(0)).moveToElement(dropdownlists.get(6)).release().build().perform();
If you want to select multiple option from your list try this (it will select first 3 elements):
List<WebElement> elements = driver.findElements(By.xpath("//select[#name='multiselectdropdown']/option"));
for(int i = 0; i < 3; i++) {
new Actions(driver).keyDown(Keys.CONTROL).click(elements.get(i)).keyUp(Keys.CONTROL).perform();
}
ButtonUp (or release()) should be the next button-action following a ButtonDown (or clickAndHold()) button-action (see Appium notes for ButtonDown documentation). Your code performs two consecutive clickAndHolds() followed by a click() without performing a release(). It should be something like:
WebDriver driver=new FirefoxDriver();
driver.get("http://startingwithseleniumwebdriver.blogspot.com/2013/12/frmset1.html");
WebElement multiSelectDropDown=driver.findElement(By.name("multiselectdropdown"));
List<WebElement> dropdownlists = multiSelectDropDown.findElements(By.tagName("option"));
Actions builder=new Actions(driver);
builder.clickAndHold(dropdownlists.get(0)).moveTo(dropdownlists.get(6)).release().build();
While the linked documentation is not for Selenium, Appium is built on top of Selenium.

not able to select hidden link - selenium

I have to select web link when i mouse hover to particular frame in the webpage, the button(link to next page) will be visible.
WebElement mainElement = driver.findElement(By.xpath(<frame xpath >));
Actions builder = new Actions(driver);
builder.moveToElement(mainElement);
WebElement button1 = driver.findElement(By.xpath("//*[#id='currentSkills']/div[1]/div/a"));
builder.moveToElement(button1).click().perform();
I am still unable to select the particular link
when i execute, the following error am getting
org.openqa.selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 131 milliseconds
But when i hover mouse pointer to the particular frame during AUT(just to move to particular frame without clicking anything), then test is executing sucessfully.
I know this can be handled by JS. But i want to find out is there any solution within selenium webdriver
On some of the drivers for the various browsers, sometimes custom actions like this won't work unless you explicitly enable native events at the time you create the driver:
FirefoxProfile profile = new FirefoxProfile();
profile.setEnableNativeEvents(true);
driver = new FirefoxDriver(profile);
or another method of setting it at the time you create DesiredCapabilities for a remote driver
DesiredCapabilities desiredCapabilities = DesiredCapabilities.internetExplorer();
desiredCapabilities.setCapability("nativeEvents", true);
When using Action Chains I have always found it more reliable to perform all the actions in the same chain. As such, rather than 'pausing' to find the now revealed element, try doing so within the chain.
WebElement mainElement = driver.findElement(By.xpath(<frame xpath >));
Actions builder = new Actions(driver);
builder.moveToElement(mainElement).moveToElement(driver.findElement(By.xpath("//*[#id='currentSkills']/div[1]/div/a"))).click().perform();
Hopefully that will help.
FirefoxProfile profile = new FirefoxProfile();
profile.setEnableNativeEvents(true);
driver = new FirefoxDriver(profile);
WebElement searchBtn = driver.findElement(By.xpath(""));
WebElement searchBtn1 = driver.findElement(By.xpath(""));
Actions action = new Actions(driver);
action.moveToElement(searchBtn).moveToElement(searchBtn1).click().build().perform();
My View here is Need to Perform Mouse Over on the Object to make it visible and then click on that element.
WebElement mainElement = driver.findElement(By.xpath(<frame xpath >));
Actions builder = new Actions(driver);
builder.moveToElement(mainElement).moveToElement(driver.findElement(By.xpath("//*[#id='currentSkills']/div[1]/div/a"))).build().perform();
driver.findElement(By.xpath("//*[#id='currentSkills']/div[1]/div/a")).click();
Please Let me know is the above scripting is working or not.

Test autocomplete with Selenium webdriver

I have a text box in which when I type one letter say 's' , it displays a list of results ( like google search) .
I am using latest selenium webdriver with java.
I have tried
sendKeys("s"),
JavascriptLibrary jsLib = new JavascriptLibrary();
jsLib.callEmbeddedSelenium(driver, "doFireEvent", driver.findElement(By.id("assetTitle")), "onkeyup");
jsLib.callEmbeddedSelenium(driver, "doFireEvent", driver.findElement(By.id("assetTitle")), "onblur");
jsLib.callEmbeddedSelenium(driver, "doFireEvent", driver.findElement(By.id("assetTitle")), "onclick");
jsLib.callEmbeddedSelenium(driver, "doFireEvent", driver.findElement(By.id("assetTitle")), "onmouseup");
driver.findElement(By.id("assetTitle")).sendKeys(Keys.ENTER);
None of these work even after adding wait after each of the steps.
Any suggestions?
Thanks.
Update :-
WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com");
WebElement query = driver.findElement(By.name("q"));
query.sendKeys("s");
driver.findElement(By.xpath("//table[#class='gssb_m']/tbody/tr/td/div/table/tbody/tr/td/span")).click();
driver.findElement(By.name("btnG")).click();
Update 2 : -
WebDriver driver = new FirefoxDriver();
driver.get("http://www.kayak.com/");
WebElement query = driver.findElement(By.name("destination"));
query.sendKeys("s");
Update 3 :-
I tried with Selenium 1 and the fireevent method works by passing parameter as 'keydown'. This should be a temporary workaround for now.
WebDriver driver = new FirefoxDriver();
driver.get("http://www.kayak.com/");
DefaultSelenium sel = new WebDriverBackedSelenium(driver,"http://www.kayak.com/");
sel.type("//input[#id='destination']", "s");
sel.fireEvent("//input[#id='destination']", "keydown");
I found a workaround about this. My problem was:
Selenium inputted 'Mandaluyong' to an auto-suggest location field
The auto-suggest field appears together with the matched option
Then selenium left the auto-suggest drop-down open not selecting the matched option.
What I did was:
driver.findElement(By.name("fromLocation")).sendKeys("Mandaluyong");
driver.findElement(By.name("fromLocation")).sendKeys(Keys.TAB);
This is because on a manual test, when I try to press TAB key, two things were done by the system:
Picks the matched option from the auto-suggest drop-down
Closes the auto-suggest drop-down
I believe you are testing auto-suggest here (not auto-complete)
Steps I follow -
Enter something in the input field
Click on the suggestion you want to choose. (You can find the xpath using some tools like Firebug with Firepath, Chrome, etc.)
Verify the text in the input field is same as expected.
This should be a temporary workaround for now.
WebDriver driver = new FirefoxDriver();
driver.get("http://www.kayak.com/");
DefaultSelenium sel = new WebDriverBackedSelenium(driver,"http://www.kayak.com/");
sel.type("//input[#id='destination']", "s");
sel.fireEvent("//input[#id='destination']", "keydown");

Categories