如何使用Selenium和Java从Google地图的自动建议中选择地址。

huangapple go评论71阅读模式
英文:

How to choose an address from the auto suggestion of google map using Selenium and Java

问题

我正在自动化一个用于注册用户信息的网页。该页面要求在输入框中输入地址,然后Google地图将列出正确的地址。我必须选择Google地图上的地址。如何在Selenium中实现这一点。下面是该功能的截图。

如何使用Selenium和Java从Google地图的自动建议中选择地址。

以下是我在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.

如何使用Selenium和Java从Google地图的自动建议中选择地址。

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(&quot;webdriver.gecko.driver&quot;, &quot;/home/avionerman/Documents/Java/geckodriver&quot;);
                WebDriver driver = new FirefoxDriver();

                driver.get(&quot;https://app.getjarvis.com.au/sign-up&quot;);
                driver.manage().window().maximize();
                driver.findElement(By.id(&quot;addressInput&quot;)).click();
                driver.findElement(By.id(&quot;addressInput&quot;)).sendKeys(&quot;Indian Drive, Keysborough VIC, &quot;);
                driver.findElement(By.id(&quot;addressInput&quot;)).click();

                try {
                    Thread.sleep(3000);
                    driver.findElement(By.id(&quot;addressInput&quot;)).sendKeys(Keys.ARROW_DOWN);
                } catch (InterruptedException e) {
                    System.out.println(&quot;Error: &quot; + e);
                }
        }
    }

Inside the Try/Catch functionality, I have this line:

    driver.findElement(By.id(&quot;addressInput&quot;)).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&#39;s &quot;Indian Drive, Keysborough VIC, &quot; instead of &quot;Indian Drive, Keysborough VIC, Australia&quot; 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(&quot;https://app.getjarvis.com.au/sign-up&quot;);
    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector(&quot;input#addressInput&quot;))).sendKeys(&quot;Indian Drive, Keysborough VIC, Australia&quot;);
    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector(&quot;span.pac-item-query&gt;span.pac-matched&quot;))).click();
    
  • xpath:

    driver.get(&quot;https://app.getjarvis.com.au/sign-up&quot;);
    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath(&quot;//input[@id=&#39;addressInput&#39;]&quot;))).sendKeys(&quot;Indian Drive, Keysborough VIC, Australia&quot;);
    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath(&quot;//span[@class=&#39;pac-item-query&#39;]/span[@class=&#39;pac-matched&#39;]&quot;))).click();
    
  • Browser Snapshot:

如何使用Selenium和Java从Google地图的自动建议中选择地址。

huangapple
  • 本文由 发表于 2020年5月4日 01:36:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/61578924.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定