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

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.

Related

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());

Selenium click not always working

I have some tests which click on a tab, however the click is not always performed.
The xpath is correct as most of the times the test works
It is not a timing issue as I ve used thread.sleep() and other methods to ensure that the element is visible before clicking
The test believes that it is performing the click as it is not throwing an ElementNotFoundException or any other exceptions when 'performing' the click. The test fails later on after the click since the tab content would not have changed.
Further Info
I am using Selenium 2.44.0 to implement tests in Java which run on Chrome 44.0.2403.107 m.
Is there something else that I can do or could this be an issue with selenium?
There are several things you can try:
an Explicit elementToBeClickable Wait:
WebDriverWait wait = new WebDriverWait(webDriver, 10);
WebElement button = wait.until(ExpectedConditions.elementToBeClickable(By.id("myid")));
button.click()
move to element before making a click:
Actions actions = new Actions(driver);
actions.moveToElement(button).click().build().perform();
make the click via javascript:
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("arguments[0].click();", button);
you can go with linkText if the tab name contains any unique string. And make sure your tab is not dynamic. It should be visible in source code(manual source code(ctrl+u)).
The following method work for me
WebElement button = SeleniumTools.findVisibleElement(By.cssSelector("#cssid"));
Actions actions = new Actions(driver);
actions.moveToElement(button).click().build().perform();
I have a similar problem. Tried all solutions from the top answer. Sometimes they work, sometimes don't.
But running code in an infinite loop works always.
For example, we need to click on element-two which is not visible until element-one is clicked.
WebDriverWait wait = new WebDriverWait(webDriver, 10);
while (true){
try {
WebElement elementOne =
wait.until(ExpectedConditions.elementToBeClickable(By.id("element-one")));
elementOne.click();
WebElement elementTwo =
wait.until(ExpectedConditions.elementToBeClickable(By.id("element-two")));
elementTwo.click();
break;
} catch (Exception e){
//log
}
}
I have a similar problem. Here is my solution:
table_button = driver.find_element(By.XPATH, insert your xpath)
try:
WebDriverWait(driver, 15).until(EC.element_to_be_clickable(table_button)).click()
except WebDriverException as e:
print('failed')
print(e)
Through code above, you can find the error message if your button is not clickable.
For example, my error message is 'nosuchelement' and 'clcik is not clickable', then I got back to check the table_button.accessible_name, found it print a 'null' value, so that means my XPATH is incorrect.

Cannot click element with IEDriverServer

I have an element on a web page that only becomes visible after clicking its parent element. So after clicking a demo in a list of demo's, a row of icons which represent actions for the selected demo is revealed. The following code works fine with both webdriver and chromedriver:
demo.click(); //click demo
waitForElementIsDisplayed(demoReservation_btn); //wait until reservation icon is displayed
demoReservation_btn.click(); //click icon
Originally i was getting a StaleElementReferenceException and i attempted to fix this by having a try/catch block within a while loop that would continue looping until the icon was clicked. This caused IEDriverServer to crash after a couple of loops.
I have also tried wrapping it up in an Action like so:
Action action = new Action(driver);
action.click(demo).click(demoReservation_btn).build().perform()
This results in a NoSuchElementException.
I know there are some problems mentioned in the documentation about browser focus and hovering over elements, but i dont believe this is the problem. I have tried a couple of other things like adding moverToElement to the action, hovering over the element but have had no success with these. I believe one possible solution is to use a javascript executor, but i would like to avoid this approach if possible, any other suggestions?
EDIT
IEDriverServer setup:
File file = new File("IEDriverServer.exe");
System.setProperty("webdriver.ie.driver", file.getAbsolutePath());
driver = new InternetExplorerDriver();
driver.manage().window().maximize();
return driver;
Try disabling Native events of IE
DesiredCapabilities cap = DesiredCapabilities.internetExplorer();
cap.setCapability("nativeEvents",false);
driver = new InternetExplorerDriver(cap);
I had better result using that in C# version. Read this to learn why you may need to do this.

Not able to select element from drop down list

I am facing a problem that i am not able to select element from drop down list to proceed further.
The URL for reference site is "http://www.rechargeitnow.com/needrecharge.jspx"
I tried the code below but didn't got success.
//WebDriverWait wait = new WebDriverWait(driver, 10);
//WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id("someid")));
WebDriverWait wait= new WebDriverWait(driver, 10);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id("opId_div")));
//driver.findElement(By.cssSelector("select[id='operatorid']")).sendKeys("Airtel");;
//driver.findElement(By.linkText("mobile")).sendKeys("Airtel");
//driver.findElement(By.xpath("//*[#id='oprauto']")).click();
//driver.findElement(By.xpath("/html/body/div[2]/div/div[6]/div/div/div/div/div[2]/div/div/div/div/div[2]/select/option[5]")).findElement(By.name("Airtel"));
//operator.selectByIndex(1);
//driver.findElement(By.xpath("//*[#id='oprauto']")).sendKeys("Airtel");
driver.findElement(By.xpath("/html/body/div[2]/div/div[6]/div/div[3]/ul/li[3]/img")).click();
//Select operator=new Select(driver.findElement(By.id("operatorid")));
//operator.getOptions();
//operator.selectByVisibleText("Airtel");
driver.findElement(By.tagName(" mobile no. ")).sendKeys("9001785845");
driver.findElement(By.id("transSubscriptionNoID")).sendKeys("9001457868");
//driver.findElement(By.cssSelector("img[id='btn']")).submit();
//driver.findElement(By.id("btn")).submit();
driver.findElement(By.xpath("//*[#id='btn']"));
I am not familiar with Java but I was able to easily accomplish this in Ruby with the following code.
$driver.find_element(:xpath, ".//*[#id='input_dropdown']/div[1]/img").click
$driver.find_element(:link, "T24").click
You can replace "T24" with any of the other options available on the dropdown.
Hopefully this answers half of your question at least and someone will be able to translate this into Java.
So the combo drop down is not a select box, its an unordered list, wrapped around an input. You would need to click on the drop down icon and then click the element you want. Here is some dirty code that WORKS :). It selects "Idea" from the drop down.
WebDriver driver = new FirefoxDriver();
WebDriverWait wait = new WebDriverWait(driver, 300);
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.get("http://www.rechargeitnow.com/needrecharge.jspx");
WebElement dropDownArrow = driver.findElement(By
.id("input_dropdown"));
dropDownArrow.click();
WebElement option = wait.until(ExpectedConditions
.elementToBeClickable(By.linkText("Idea")));
option.click();
The following code works. This is not what I wanted to provide you with as a code. But works none the less. You can replace Airtel with any other text present in the drop down.
driver.findElement(By.id("oprauto")).sendKeys("Airtel");
driver.findElement(By.xpath("//ul/li/a/strong[text() = 'Airtel']")).click();

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