The website I am testing for has a textbox to enter location. When I enter a character, Google API sends the list of locations matching this character below the textbox. here is the snapshot of autocomplete options
I am trying to automate this scenario in selenium. But the problem is when I do textbox.sendkeys("text"), the populating the locations list from Google API does not happen.
I have tried implicit and explicit wait, with timer upto 200s, still it didn't work. I tried, click() before sendkeys(), KEYS.Arrow_down after sendkeys() and also mouseclick. But it still doesn't seem to work.
Please provide your suggestions.
Here is my test code:
public void add_inv() throws Exception{
commands.implicitwait(driver);
Inventory.btn_addinventory(driver).click();
Inventory.btn_addlocation(driver).click();
Inventory.txt_locAddr(driver).click();
commands.implicitwait(driver);
Inventory.txt_locAddr(driver).click();
Inventory.txt_locAddr(driver).sendKeys("bangalore", Keys.ARROW_DOWN);
commands.setDefaultTimeout(100);
commands.implicitwait(driver);
String loctext = driver.findElement(By.xpath("html/body/div[3]/div[1]/span[3]")).getText();
System.out.println("loctext: "+loctext);
}
try with action class as given below.
public void add_inv() throws Exception{
commands.implicitwait(driver);
Inventory.btn_addinventory(driver).click();
Inventory.btn_addlocation(driver).click();
Inventory.txt_locAddr(driver).click();
commands.implicitwait(driver);
Inventory.txt_locAddr(driver).click();
//Inventory.txt_locAddr(driver).sendKeys("bangalore", Keys.ARROW_DOWN);
new Actions(driver).sendKeys(Inventory.txt_locAddr(driver),"bangalore").perform();
String loctext = driver.findElement(By.xpath("html/body/div[3]/div[1]/span[3]")).getText();
System.out.println("loctext: "+loctext);
}
I had a similar problem in Edge browser. The solution is to Click the searchtext box and then send keys.
driver.click();// on the search box
Driver.sendkeys("ABC").
For me the tabs or any keys after the sendkeys had not worked.
Thanks
Related
This question already has an answer here:
How to automate Google Home Page auto suggestion?
(1 answer)
Closed 3 years ago.
I am trying to verify if the selected value in an AutoSuggest Dropdown is correct using Selenium. But using the getText() doesn't return any results.
Below is my code
public class AutoSuggestDropdownsTest {
public static void main(String[]args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "/resources/executables/chromedriver") ;
WebDriver driver = new ChromeDriver();
driver.manage().deleteAllCookies();
driver.get("https://example.com/");
driver.findElement(By.xpath("//*[#class='stamp remove-sign']")).click();
driver.findElement(By.xpath("//input[#id='or-search']")).sendKeys("to");
driver.findElement(By.xpath("//input[#id='or-search']")).sendKeys(Keys.DOWN);
driver.findElement(By.xpath("//input[#id='or-search']")).sendKeys(Keys.DOWN);
driver.findElement(By.xpath("//input[#id='or-search']")).sendKeys(Keys.DOWN);
System.out.println(driver.findElement("//input[#id='or-search']")).getText());
System.out.println("hello");
driver.quit();
}
Usually autosuggestion results take some time to appear, so if you do the sendKeys(Keys.DOWN) immediately after sending the to it is very likely that no autosuggestions appeared yet, and this way the DOWN key has no effect. To check if this is the case (for development purpose of the test only) put a sleep 10s, and check if with the sleep the text you think should appear, actually appears.
if it does appear, then you should implement some kind of smart waiting for the results to appear.
if it doesn't appear, maybe there is another problem, in which case it would be very useful if you could post the html of the page after the autossugestions appeared.
Using selenium, when trying to enter username and password in login form, sometimes the text is entered on the same field. Username and password are having unique identifier.
For sending keys, the following steps are done.
sendKeys(By.id("login_username"), "abc");
sendKeys(By.id("login_password"), "efg");
public void sendKeys(By locator, String text) {
WebElement element = findElement(locator);
if(element != null) {
element.clear();
element.sendKeys();
}
}
public WebElement findElement(By locator) {
return wait(org.openqa.selenium.support.ui.ExpectedConditions.presenceOfElementLocated(locator));
}
public WebElement wait(ExpectedCondition<WebElement> condition) {
Wait<WebDriver> wait = new FluentWait<WebDriver>(driver).pollingEvery(1, TimeUnit.SECONDS)
.withTimeout(60, TimeUnit.SECONDS)
.ignoreAll(Arrays.asList(NoSuchElementException.class, TimeoutException.class));
return wait.until(condition);
}
But somehow, when entering text, both the username and password texts are getting typed onto the password field. This is not happening always though. Not able to understand what could possibly go wrong or what to check. Any suggestions...
platform: Ubuntu 16.04.1 LTS 64-bit
chromedriver version: 2.25
chrome browser version: 55.0.2883.87
Thanks in advance.
The sendKeys() action performs the following steps:
Gets the coordinates of the element
Clicks at the coordinates it got (using a mouse action)
"Types" the text (that will be received by whatever element is currently focused)
It can go wrong if the element can't be focused when starting the action (because it's disabled for example), or if the coordinates change between getting the coordinates and clicking/focusing the element (because the layout is still changing).
Another common cause can be onClick action hooked on the element, that can lead to race conditions. Without seeing the actual page, it is possible that after selenium clicks, an onClick action is still working when the text is being typed. This basically looks like this:
Selenium clicks
onClick action starts (element might get focused only after it finishes)
Selenium starts typing (onClick haven't
returned yet, so the wrong elements gets the text)
the onClick
action finishes, but by that time Selenium finished too.
As a solution you can try focusing the element directly, and wait until it is really focused before sending the keys. This question might be of use for this case.
[![enter image description here][1]][1]HTML code is shown in Screenshot [![enter image description here][2]][2]
I tried with Action class
WebElement element = InspectationOrder.wd.findElement(By.xpath("//div[#class='qx-window']"));
Actions actions = new Actions(InspectationOrder.wd);
actions.moveToElement(element).click().build().perform();
but found "java.lang.NullPointerException" while i tried to move focus.
but same action code works for other area in application
Also tried with
for (String popup : wd.getWindowHandles())
{
wd.switchTo().window(popup);
}
but not working :(
May be issue with z-index but m don't have more idea about the same .
(new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver d) {
boolean bool = (Boolean) ((JavascriptExecutor)d).executeScript("return $('.modal').is(':visible') ");
return bool;
}
});
Your Dialog is actually an div in an upper layer. This codes waits up to 10 seconds until the dialog is created and show completely. Inside JavaScript is used to select the dialog and check weather is visible or not. (display: none). The result is returned. If the result is false your test will stop.
Can you please try adding an id to the div and use the below code?
driver.switchTo().frame(driver.findElement(By.id(id)));
driver.findElement(By.id(id inside popup));
Selenium latest version(Selenium 3 with GeckDriver ) automatically handles set focus.
Previously I am using selenium 2 in which we have to setfocus on new element.But in selenium 3 with GecoDriver we have no need to worry about small popups.
In my whole automation script selenium 3 handles setfocus[ Not fully but at basic level ] for me and works form me.
Use full links.
LINK-1
[LINK-2]
-Thanks
Dhaval
i am using selenium to simulate a from .The problem now in the resulat page selenium confuse in displaying the value of text : it show me "parisl" in the first textFiled and "marseile" in the second. i can't understand what's the problem
This is my code
public class SeleniumExample {
public static void main(String[] args) throws InterruptedException {
// The Firefox driver supports javascript
FirefoxProfile profile = new FirefoxProfile();
profile.setEnableNativeEvents(true);
RemoteWebDriver driver = new FirefoxDriver(profile);
// WebDriver driver = new FirefoxDriver();
// And now use this to visit Google
driver.get("http://www.voyages-sncf.com/");
// Find the text input element by its name
driver.findElement(By.name("origin_city")).sendKeys("paris");
driver.findElement(By.id("train-destination-city")).sendKeys("marseille");
Thread.sleep(4000);
driver.findElement(By.id("train-book")).click();}
}
This coce should be working. Maybe I have one advice - but I have to make wild guess and guess that you are not English:
To be able to work with my selenium scripts and have proper key events, I had to make English keyboard as my default system layout. When I had my keyboard set up to Czech (my language), some key events were not working properly.
So assuming you use French layout, I advise you to switch it to English as default keyboard
When I open that page it redirects me to http://www.tgv-europe.com/ which doesn't have such an input tag as you are looking for above.
Maybe the website has some logic that redirects according to region?
P.S Not relevant to the original question, but an advice for you is to use implicit waits instead of doing a Thread.sleep. Implicit waits will wait until the element you are looking for has appeared, unless your max time limit is reached.
I was trying to click a button on my mobile web app, using selenium web driver. The button is located, the text over the button can be derived and even the click event is performing well. But the navigation doesn't occur.
I tried with Click() method, sendKeys() method and also with script executor. But couldn't process further on.
CODE:
public class TestWeb
{
WebDriver driver;
private Selenium selenium;
#Before
public void setUp() throws Exception {
driver = new IPhoneDriver();
driver.get("http://10.5.95.25/mobilebanking");
}
#Test
public void TC() throws Exception {
System.out.println("page 1");
Thread.sleep(5000);
WebElement editbtn1 = driver.findElement(By.id("ext-comp-1018"));
String s1 = editbtn1.getText();
System.out.println(s1);
editbtn1.click();
editbtn1.sendKeys(Keys.ENTER);
((JavascriptExecutor)driver).executeScript("arguments[0].click;", editbtn1);
System.out.println("ok");
}
#After
public void tearDown() throws Exception {
System.out.println("*******Execution Over***********");
}
}
I tried click, sendKeys and ScriptExecutor separately and also combined. It is executing without any error but the navigation doesn't occur.
Does anybody can help me with some other ways to perform click function on the button?
Ram
This may not be your issue but I noticed "ext-comp-" and guess you are using extjs.
I'm using GXT and while finding by id worked for many things, on some submit buttons it didn't.
I had to use firebug in firefox to locate the element and copy the xpath.
Then I could click the element by
driver.findElement(By.xpath("//div[#id='LOGIN_SUBMIT']/div/table/tbody/tr[2]/td[2]/div/div/table/tbody/tr/td/div")).click(); // worked
It was failing silently for me too. My submit button has the id of LOGIN_SUBMIT so I don't know why the following failed but ....
driver.findElement(By.id("LOGIN_SUBMIT")).click();//failed
Edit:
Here is an exact example (case 1 of 2):
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//div[#id='gwt-debug-LOGIN_SUBMIT']")));
//wait.until(ExpectedConditions.elementToBeClickable((By.id("gwt-debug-LOGIN_SUBMIT")))); <!-- id works as well
OK so the element is found. It will timeout and throw an exception if it is not.
Still, the following fails (under firefox, works with chrome) with no error and the page does not navigate.
driver.findElement(By.xpath("//div[#id='gwt-debug-LOGIN_SUBMIT']")).click();
//driver.findElement(By.id("gwt-debug-LOGIN_SUBMIT")).click(); <-- fails too
What I have to do is:
driver.findElement(By.xpath("//div[#id='gwt-debug-LOGIN_SUBMIT']/div/table/tbody/tr[2]/td[2]/div/div/table/tbody/tr/td/div")).click();
So my experience was that even if I found the element with xpath, clicking failed unless I used a complete xpath.
Here is another exact example (case 2 of 2):
I can find an element like so:
WebElement we = driver.findElement(By.xpath("//*[#id=\"text" + i + "\"]"));
I know I have found it because I can see the text via:
we.getText();
Still selecting by the path I found it fails.
//get outta town man the following fails
driver.findElement(By.xpath("//*[#id=\"text" + i + "\"]")).click();
In this case there is not more explicit xpath to try as in case 1
What I had to do was use css:
//bingo baby works fine
driver.findElement(By.cssSelector("div#text" + i + ".myChoices")).click();
Actually, I obtained the css path via firebug than shortened it.
//this is what I recieved
html.ext-strict body.ext-gecko div#x-auto-0.x-component div#x-auto-1.x-component div#x-auto-3..myBlank div#choicePanel1.myBlank div.x-box-inner div#text3.myChoices //text3 is the id of the element I wanted to select
Whether or not you can figure out your needed xpaths and css selectors, I don't know, but I believe I experienced exactly what you did.