Get Text from Auto Suggest Dropdown not working in Selenium [duplicate] - java

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.

Related

autocomplete does not get populate after entering text from selenium

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

Handling an AngularJS popup with Selenium WD

The following picture shows a pop-up that appears when clicked on the "Random User" text; WD is not able to shift its focus to the pop-up you see.
Below is the code I employed with various trial codes commented as in
public static void teardownAppRetrieveOnce() throws InterruptedException {
synchronized(obj){
obj.wait(2000);
driver.findElement(By.xpath(".//*[#id='sidebar']/nav/ul[2]/li[3]/button[1]/span/span")).click();
System.out.println("Clicked sidebar..");
obj.wait(2000);
String winHandleBefore = driver.getWindowHandle();
**driver.switchTo().window(winHandleBefore);
driver.switchTo().findElement(By.id(".//*[#id='logoutButton']")).click();**
***driver.switchTo().activeElement().findElement(By.id(".//*[#id='logoutButton']")).click();***
System.out.println("Clicked on logout");
obj.wait(2000);
driver.switchTo().alert().accept();
obj.wait(2000);
System.out.println("Logged out...");
}
}
Please help me clarify if such pop-up means a separate new window or frame that is to be handled?
NOTE:
** means the first solution I tried that failed
*** means the second solution i tried that too failed
I have had a lot of trouble with WebDriver/AngularJS. Sometimes native Selenium methods of WebElements work - sometimes not. The best solution is to execute raw Javascript on the web page.
I your case, it would be something like this:
driver.executeScript("$x(\".//*[#id='logoutButton']\")[0].click()");

Having issue finding element Fluentlenium

I am writing some automated tests using Fluentlenium and PhantomJS. I am having trouble accessing the id "#title". The test I have written is as follows:
#Test
public void testCreateButton() {
startAppWithCallback(new F.Callback<TestBrowser>() {
public void invoke(TestBrowser browser) throws InterruptedException {
CalendarPage calendarPage = browser.createPage(CalendarPage.class);
calendarPage.withDefaultUrl(BASE_URL);
calendarPage.go();
calendarPage.selectCreateButton();
calendarPage.typeTitle("Java Fundamentals");
browser.await().atMost(3, TimeUnit.SECONDS);
}
});
}
The test is running, and seems to be able to select the Create button, which should then open up a modal window, but for some reason it is having trouble seeing the id on this modal. The error message that I get is as follows:
org.openqa.selenium.NoSuchElementException: No element is displayed or enabled. Can't set a new value.
Is there something I am not doing when it comes to accessing the id on the modal window? Any help at all would be much appreciated.
Usually modal windows take some time to attach to the DOM of the page you are accessing. Though you have added 3 seconds to wait for the element to appear/ attach to the DOM but the time is not sufficient. I would not recommend to increase the timeout but would recommend to wait until for the element to appear and then move forward. for e.g. you could do following thing to wait for an element to appear on the page instead of waiting statically:
FluentWaitMatcher matcher = page.await().atMost(, TimeUnit.SECONDS).until(findPattern);

Error of display page with selenium

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.

The screen is not navigating even the click() method is executed successfully in mobile web app using selenium web driver

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.

Categories

Resources