Reputation: 185
I am Automating a web page for registering user info. That page requires t enter address in the Input box, then Google map will list down the correct address. I have to choose the google map address. How to do this in selenium. Here is the image of the function.
Here is my Code in Selenium - Java.
public void signup() throws InterruptedException {
driver.get("https://app.getjarvis.com.au/sign-up");
driver.manage().window().setSize(new Dimension(801, 721));
driver.findElement(By.id("addressInput")).click();
driver.findElement(By.id("addressInput")).click();
driver.findElement(By.id("addressInput")).sendKeys("Indian Drive, Keysborough VIC, Australia");
Thread.sleep(3000);
Select drpdwn = new Select(driver.findElement(By.id("addressInput")));
drpdwn.selectByVisibleText("Indian Drive, Keysborough VIC, Australia");
Upvotes: 2
Views: 3439
Reputation: 193298
To click()
on the pac-matched auto suggestion you need to induce WebDriverWait for the elementToBeClickable()
and you can use either of the following Locator Strategies:
cssSelector
:
driver.get("https://app.getjarvis.com.au/sign-up");
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("input#addressInput"))).sendKeys("Indian Drive, Keysborough VIC, Australia");
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("span.pac-item-query>span.pac-matched"))).click();
xpath
:
driver.get("https://app.getjarvis.com.au/sign-up");
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@id='addressInput']"))).sendKeys("Indian Drive, Keysborough VIC, Australia");
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//span[@class='pac-item-query']/span[@class='pac-matched']"))).click();
Browser Snapshot:
Upvotes: 0
Reputation: 1119
if you know that the first option in the address dropdown is what you want, you can do this after you do the send keys:
driver.findElements(By.cssSelector(".pac-item")).get(0).click();
Upvotes: 3
Reputation: 1846
I tried something that is working only if the text you give is unique:
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.By;
import org.openqa.selenium.support.ui.Select;
import java.lang.*;
import org.openqa.selenium.WebElement;
import java.util.*;
class Main {
public static void main(String args[]) {
System.setProperty("webdriver.gecko.driver", "/home/avionerman/Documents/Java/geckodriver");
WebDriver driver = new FirefoxDriver();
driver.get("https://app.getjarvis.com.au/sign-up");
driver.manage().window().maximize();
driver.findElement(By.id("addressInput")).click();
driver.findElement(By.id("addressInput")).sendKeys("Indian Drive, Keysborough VIC, ");
driver.findElement(By.id("addressInput")).click();
try {
Thread.sleep(3000);
driver.findElement(By.id("addressInput")).sendKeys(Keys.ARROW_DOWN);
} catch (InterruptedException e) {
System.out.println("Error: " + e);
}
}
}
Inside the Try/Catch functionality, I have this line:
driver.findElement(By.id("addressInput")).sendKeys(Keys.ARROW_DOWN);
In practice, when you type the text you need then if the option is only one, you can say to the bot to press the arrow down in order to choose the Option. As you can see in my example the text is not ready. It's "Indian Drive, Keysborough VIC, " instead of "Indian Drive, Keysborough VIC, Australia" because I wanted to see the suggestion. So, to conclude if you will enter your text and the result is only one (unique) this code will always select the option from the list by pressing the arrow down button (sendKeys(Keys.ARROW_DOWN)).
If you need to escalate it further here is the official Google page for this suggestion generator.
Upvotes: 0