英文:
How to choose an address from the auto suggestion of google map using Selenium and Java
问题
我正在自动化一个用于注册用户信息的网页。该页面要求在输入框中输入地址,然后Google地图将列出正确的地址。我必须选择Google地图上的地址。如何在Selenium中实现这一点。下面是该功能的截图。
以下是我在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");
}
英文:
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");
答案1
得分: 2
如果您知道地址下拉框中的第一个选项是您想要的,您可以在执行“send keys”操作后进行以下操作:
driver.findElements(By.cssSelector(".pac-item")).get(0).click();
英文:
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();
答案2
得分: 0
我尝试了一些只在提供的文本是唯一的情况下才起作用的东西:
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);
}
}
}
在Try/Catch功能中,我有这行代码:
driver.findElement(By.id("addressInput")).sendKeys(Keys.ARROW_DOWN);
实际上,当您输入所需的文本时,如果选项只有一个,您可以告诉机器人按下箭头向下键以选择该选项。如您在我的示例中所见,文本尚未准备好。它是“Indian Drive, Keysborough VIC, ”而不是“Indian Drive, Keysborough VIC, Australia”,因为我想看到建议。所以,总之,如果您输入的文本只有一个(唯一的),这段代码将始终通过按下箭头向下键(sendKeys(Keys.ARROW_DOWN))从列表中选择选项。
如果您需要进一步升级,此处 是官方的谷歌页面,用于此建议生成器。
<details>
<summary>英文:</summary>
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][1] is the official Google page for this suggestion generator.
[1]: https://developers.google.com/maps/documentation/javascript/examples/places-autocomplete-addressform
</details>
# 答案3
**得分**: 0
```markdown
要在匹配的自动建议上进行`click()`操作,您需要使用[WebDriverWait](https://stackoverflow.com/questions/48989049/selenium-how-selenium-identifies-elements-visible-or-not-is-is-possible-that-i/48990165#48990165)来等待`elementToBeClickable()`,然后可以使用以下任一[定位策略](https://stackoverflow.com/questions/48369043/official-locator-strategies-for-the-webdriver/48376890#48376890):
- `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();
- 浏览器快照:
[![jarvis][1]][1]
[1]: https://i.stack.imgur.com/pGW1y.png
英文:
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:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论