Selenium test does not work for autocomplete when google reverse view

I am missing something very elementary. From all that I could collect, I think I need a list id that crashes as I type. As soon as I have an id , I think I can get the list and iterate over it.

AutoFill Web Code:

 <div class="col-md-12"> <label>Google Map Location*</label> <input id="searchTextField" type="text" class="form-control" placeholder="Enter a location" autocomplete="on" required="required"> <input id="latitude" type="text" style="display: none;" class="form-control" value=""> <input id="longitude" type="text" style="display: none;"class="form-control" value=""> <p class="help-block" style="color: #737373;">Instruction: Please drag the marker to get exact location of the IT park.</p> </div> 

javascript to initialize js code

  var input = document.getElementById('searchTextField'); var autocomplete = new google.maps.places.Autocomplete(input); autocomplete.bindTo('bounds', map); 

Selenium Code:

 autocompleteElement = God.getWebElementById("searchTextField"); autocompleteElement.sendKeys("Airport"); List<WebElement> autoCompleteList = Initialize.getInstance().getDriver().findElements( By.className("form-control"); for (WebElement autocompleteItem : autoCompleteList) { if (autocompleteItem.getText().contains("Pune Airport")) { autocompleteItem.click(); } break; } 

I get autocompleteItem empty or empty.

Change 1

I am new to javascript and I am inheriting this code base. I don’t understand how to assign a locator container OR a id so that I can iterate through WebElement and click on my desired result.

Edit 2

I added a snapshot of the autocomplete check item. The answers below relate to xpath ("something very complicated for me"). What can I replace xpath in my specific case?

OR

What do you need to help me get a dropdown list?

autofill dropdown snapshot

Edit 3

I used the class- form-control class name like this

 List<WebElement> autoCompleteList = waitForElementByClass( "form-control"); System.out.println("autocomplete list size " + autoCompleteList.size()); for (WebElement autocompleteItem : autoCompleteList) { if (autocompleteItem.getText().contains("Wadgaon Sheri")) { System.out.println("auto complete selected " + autocompleteItem.getText()); autocompleteItem.click(); break; } else System.out.println("no match, tagname:" + autocompleteItem.getTagName() + "point:" + autocompleteItem.getLocation()); } 

And got the following result. Note: autocompleteItem.getText returns an empty string.

 autocomplete list size 16 no match, tagname:inputpoint:(255, 200) no match, tagname:selectpoint:(255, 274) no match, tagname:inputpoint:(255, 433) no match, tagname:selectpoint:(255, 509) no match, tagname:inputpoint:(255, 553) no match, tagname:inputpoint:(330, 553) no match, tagname:inputpoint:(255, 627) no match, tagname:textareapoint:(525, 200) no match, tagname:textareapoint:(525, 324) no match, tagname:inputpoint:(525, 450) no match, tagname:inputpoint:(525, 526) no match, tagname:inputpoint:(525, 602) no match, tagname:inputpoint:(525, 678) no match, tagname:inputpoint:(796, 200) no match, tagname:inputpoint:(0, 0) no match, tagname:inputpoint:(0, 0) 

Now I try with the class name pac-container pac.logo , and then I try with div.pac-container.pac-logo too :). Remember my javascript skills are not too great.

pac-container pac.logo

Edit 4 Now I tried the class name as pac-container pac.logo , pac-container.pac-logo , div.pac-container.pac.logo and pac-container , the result

wait.until (ExpectedConditions.visibilityOfElementLocated (By.className (byclass)));

is that the code hangs here

If I replaced the string

wait.until (ExpectedConditions.elementToBeSelected (God.getWebElementByClassNae (byclass)));

I get a NoElementFoundException .

So, my closest bet on solving this problem is to find the class name of the damned drop-down list.

 public List<WebElement> waitForElementByClass(String byclass) { WebDriverWait wait = new WebDriverWait(God.getCurrentBrowser(), 2000); List<WebElement> elements = null; boolean waitForElement = true; System.out.print("Waiting for " + byclass); do { System.out.print("."); try { //wait.until(ExpectedConditions.visibilityOfElementLocated(By.className(byclass))); wait.until(ExpectedConditions.elementToBeSelected(God.getWebElementByClassNae(byclass))); waitForElement = false; System.out.println("found"); elements = God.getWebElementsByClassName(byclass); } catch (NoSuchElementException e) { waitForElement = true; } catch (TimeoutException e) { waitForElement = false; return null; } } while (waitForElement); return elements; } 
+5
source share
4 answers

Paste the code to wait for the item to appear using WebDriverWait. Example below

 WebDriverWait wait = new WebDriverWait(driver,10000); wait.until(ExpectedConditions.visibilityOfElementLocated(By.className("form-control"))); 
0
source

I think you should wait for the list after entering in searchTextField not for the input field itself. So, after entering in searchTextField you will have a list of sentences and get a locator for this. In your code, wait for this locator until it becomes visible. Let's say your locator is to automatically suggest autosuggestion-list (assuming it's an identifier), so the code snippet will look like this:

 WebDriverWait wait = new WebDriverWait(driver,120); // wait 120 seconds until element is visible wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("autosuggestion-list"))); 

Hope you could get my thought.

0
source

Try it. it will wait for the item to be loaded and it will be ready to click.

 WebDriverWait wait = new WebDriverWait(driver,60); wait.until(ExpectedConditions.elementToBeClickable(By.className("form-control"))); 
0
source

I tried the same script with Google Instant search , and the code

  driver.get("http://google.com"); driver.findElement(By.id("lst-ib")).sendKeys("airport"); Thread.sleep(2000); // mandatory to use in this case List<WebElement> autoSearchList=(List<WebElement>) driver.findElements(By.xpath("//*[@id='sbtc']/div[2]/div[2]/div[1]")); for ( WebElement item: autoSearchList) { if(item.getText().contains("airport jobs")) item.click(); } 

My thoughts on this: "autoSearchList" shows NULL values, even if we use either Implicit wait / Explicit wait along with certain conditions. For instant search values, only Thread.sleep() should be used.

I do not know why Selenium does not work here, but you can try using the sleep method to achieve the result.

0
source

Source: https://habr.com/ru/post/1260353/


All Articles