I am trying to type a string in capital letters (using KeyDown) into amazon.in website search bar using sendKeys() but I don't see the text on the search bar. I don't see any error. I use debug mode then I also I can find any error.
Question:
How can I resolve this?
How can I debug it myself and find issue?
For debug I put a breakpoint on below line and then use step over option to run each line.
mouseAction.moveToElement(elementLocation).build().perform();
public class MouseActions {
public static void main (String [] args){
System.setProperty ("webdriver.chrome.driver","C:\\Users\\tokci\\Documents\\Selenium\\drivers\\chromedriver_win32\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://www.amazon.in/");
Actions mouseAction = new Actions(driver);
//this mouse action works
WebElement elementLocation = driver.findElement(By.xpath("//a[#id='nav-link-yourAccount']"));
mouseAction.moveToElement(elementLocation).build().perform();
//below code does not work
WebElement keysLocation = driver.findElement(By.xpath("//input[#id='twotabsearchtextbox']"));
mouseAction.keyDown(Keys.SHIFT).moveToElement(keysLocation).sendKeys("shoes").build().perform();
}
}
keysLocation is a input element here you can use .sendKeys() without using mouseAction as below and it works :-
keysLocation.sendKeys(Keys.SHIFT, "shoes");
Hope it will help you..:)
You can try using click before you use sendKeys. moveToElement function just moves the cursor over and .click() function will ensure that the element has been selected
Try this
WebElement keysLocation = driver.findElement(By.xpath("//input[#id='twotabsearchtextbox']"));
mouseAction.keyDown(Keys.SHIFT).moveToElement(keysLocation).click().sendKeys("shoes").build().perform();
firefox driver does not respond to keydown command.
so my work around was not use actions class. Here's example of code:
driver.findElement(By.name("q")).sendKeys(Keys.SHIFT + "big");
Related
I'm quite new to Selenium HTMLUnit so I'm looking for some help in creating a basic test for a navigation button in my Spring Boot app.
I'm trying to put together some basic tests to make sure that the navigation buttons in my webapp work as expected. My approach to this is to find the required navigation button by Id, click it, then check that the current URL is the new page I was expecting to be on.
...
private HtmlUnitDriver driver = new HtmlUnitDriver(true);
...
#Test
public void navigationTest() {
driver.get(BASE_URL);
WebElement button = driver.findElement(By.id("navigation_button_id"));
button.click();
String currentUrl = driver.getCurrentUrl();
Assert.assertThat(currentUrl, is(BASE_URL + "some_other_page"));
driver.close();
}
I've attempted to use the .click() method (shown above), I've also attempted to use an Actions object instead of the simpler .click() method (see below) to perform the navigation but this still did not work.
Actions actions = new Actions(driver);
actions.click(button).build().perform();
The behaviour at the moment is that the URL is not changing from the BASE_URL (e.g. http://localhost:8080), I have verified that it works manually (clicking around in person) but I can't get the test to click the button and tell me that the URL has changed (showing that the user has been taken to the new page e.g. http://localhost:8080/some_other_page).
Can anyone offer some advice to get this working? I just need the simplest means of testing basic navigation is working in HTMLUnit.
I would suggest you to use below code for Navigation purpose :
To go forward :
driver.naviagte().forward();
To go backward :
driver.navigate().back();
For solving your issue , when you are going forward/backward : Wait for some element by using this code :
WebDriver wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated("some element locator"));
Then get the currentUrl by using driver.getCurrentUrl(); code.
Hope this will help you to resolve your issue.
Please let me know if you have any concerns related to this.
The way I remember doing it is to wait/ assert for the new page to load. If you can't do that, add a wait of few seconds before getCurrentUrl()
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.
Scenario: Open website, obtain a reference to Webelement "About" , click About , navigate back and use the variable reference again -- Results in StaleElementReference Exception. This happens only with Selenium Java, however when using Watir, it works fine. Both the code snippets are posted below. Anyone got a damn clue what is going on ?
# Below Java code produces StaleElementReferenceException
public class StaleElementException {
public static void main(String[] args) {
ChromeDriver driver = new ChromeDriver();
driver.get("http://seleniumframework.com");
WebElement about = driver.findElementByLinkText("ABOUT");
System.out.println(about.getText());
about.click();
driver.navigate().back();
System.out.println(about.getText());
}
}
#Below Ruby Watir Code works fine
require 'watir-webdriver'
#browser = Watir::Browser.new :chrome
#browser.goto "http://seleniumframework.com"
about = #browser.link(text: 'ABOUT')
puts about.text
about.click
#browser.back
puts about.text
Watir automatically does another find element call after the refresh, which webdriver does not do, so you need to do what Saifur suggested.
I am not sure how Watir works here. But if you find an element click on it it navigates you to a different page and the DOM refreshes. You therefore going back with driver.navigate().back(); and try to use same about element to perform your action which is not valid anymore. The DOM refreshed means the reference to the element is lost and that's not a valid element anymore. What you should be doing is finding the same element again on the fly and perform your action. The complete code should look like the following:
public class StaleElementException {
public static void main(String[] args) {
ChromeDriver driver = new ChromeDriver();
driver.get("http://seleniumframework.com");
WebElement about = driver.findElementByLinkText("ABOUT");
System.out.println(about.getText());
about.click();
driver.navigate().back();
System.out.println(driver.findElementByLinkText("ABOUT").getText());
}
}
Note: The change in last line
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.
The following code tests an autocomlete box of a webpage:
public class Test {
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver","chromedriver\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://www..............com");
driver.switchTo().frame("mainFrame");
WebDriverWait waitst = new WebDriverWait(driver, 120);
waitst.until(ExpectedConditions.visibilityOfElementLocated(By.name("sourceTitle")));
WebElement sourceTitle = driver.findElement(By.name("sourceTitle"));
WebElement small = driver.findElement(By.cssSelector("li#nameExampleSection label + small"));
sourceTitle.sendKeys("Times");
Thread.sleep(5000);
Actions actions = new Actions(driver);
actions.click(small).perform();
}
}
Why doesn't the autosuggest box load? IMPORTANT: try to type in "..........." manually ... the autocomplete box will load perfectly fine!!! So, why does not cssSelector work, why doesn't it load the autocomplete box?
How come an automated input does not allow for autocomplete options BUT a manual input does???
PS: I also tried fireEvent, sendKeys but nothing works.
I tried your code, it does exactly what the manual walkthough does. "Associated Press, The" returns only a "No Match, please try sources". In your code you then try to click on the next form list item, not the results popup. The autosuggest popup is dynamically populated at the top of your html page positioned under the input form. The following code does select the first option on your drop down.
#Test
public void test() throws InterruptedException {
WebDriver driver = new ChromeDriver();
driver.get("http://www.lexisnexis.com/hottopics/lnacademic/?verb=sf&sfi=AC00NBGenSrch");
driver.switchTo().frame("mainFrame");
WebDriverWait waitst = new WebDriverWait(driver, 0);
waitst.until(ExpectedConditions.visibilityOfElementLocated(By.name("sourceTitle")));
WebElement sourceTitle = driver.findElement(By.name("sourceTitle"));
sourceTitle.sendKeys("Times");
Thread.sleep(5000);
WebElement firstItem = driver.findElement(By.xpath("//*[#class='auto_suggest']/*[#class='title_item']"));
firstItem.click();
}
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
Hope this helps.
// allow autopuplation value to fill the text box.
// wait for 6 sec make sure auto value is inserted
thread.sleep(6000L);
// clear auto filled value
driver.findElement(By.name("txtBox")).clear();
driver.findElement(By.name("txtBox")).sendKeys("value");
Try first clicking on the Input textbox.
This will trigger the auto populating dropdown box and then enter the required value using sendKeys
Either you can use tab or enter to Escape the scenario or,it is not possible by selenium if it is mandatory fields. (sad)