Java 如何处理硒中的自动建议

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23863425/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 08:36:51  来源:igfitidea点击:

how to handle Auto suggestion in selenium

javaseleniumselenium-webdriver

提问by Ravi Anand

//for source
 WebElement from= driver.findElement(By.id("autocomplete_source"));
      from.clear();
      from.sendKeys(FromCity);
      Thread.sleep(3000);
      Actions builder=new Actions(driver);
      builder.moveToElement( from.findElement(By.xpath("//*[@class='acResults']/ul/li[1]/span"))).click().build().perform();

//for destination

 WebElement to=driver.findElement(By.id("autocomplete_dest"));
      to.clear();
      driver.findElement(By.id("autocomplete_dest")).sendKeys(ToCity);
builder.sendKeys(Keys.ARROW_DOWN).click().build().perform();
      WebDriverWait wait=new WebDriverWait(driver, 90);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*        [@class='acResults']/ul/li/span")));
     to=driver.findElement(By.cssSelector(".acResults>ul>li>span"));
      Thread.sleep(2000);
          to.click();

/* there are two text box source and destination,source is getting handled but destination is not working. when i am passing Chennai in destination list is showing arrow down is working but it is not selecting and giving error locator is not visible */

/* 有两个文本框源和目标,源正在处理但目标不起作用。当我在目的地列表中通过钦奈时显示向下箭头正在工作但它没有选择并且给出错误定位器不可见*/

采纳答案by Deepak

provide HTML. I tried auto suggestion on redbus.in & is working without using Action class. Here is a code which works for me on FF/Chrome-

提供 HTML。我在 redbus.in 上尝试了自动建议,并且在不使用 Action 类的情况下工作。这是一个在 FF/Chrome 上对我有用的代码-

driver.get("http://www.redbus.in/");
driver.findElement(By.xpath(".//*[@id='txtSource']")).sendKeys("pune");
driver.findElement(By.xpath(".//*[@id='txtDestination']")).sendKeys("Chennai");

Please provide url or html so we can dig more in to it.

请提供 url 或 html,以便我们可以深入了解它。

Below code is working for me on FF(http://in.via.com/bus-tickets)-

下面的代码在 FF 上对我有用(http://in.via.com/bus-tickets)-

driver.get("http://in.via.com/bus-tickets/");
WebElement from = driver.findElement(By.xpath(".//*[@id='autocomplete_source']"));
WebElement to =driver.findElement(By.xpath(".//*[@id='autocomplete_dest']"));
from.clear();
from.sendKeys("Pune");
to.clear();
to.sendKeys("Chennai");
WebDriverWait wait=new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("html/body/div[9]/ul/li")));
driver.findElement(By.xpath("html/body/div[9]/ul/li")).click();

//clicking submit

//点击提交

driver.findElement(By.xpath(".//*[@id='bookingDiv']/div[21]/input")).click();

回答by Sitam Jana

Tried with the following code. Worked for me:

尝试使用以下代码。为我工作:

WebDriver driver = new FirefoxDriver();
driver.get("http://in.via.com/bus-tickets");

driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

driver.findElement(By.xpath("//input[@id='autocomplete_source']"))
    .clear();

driver.findElement(By.xpath("//input[@id='autocomplete_source']"))
    .sendKeys("Chen");

driver.findElement(
    By.xpath("//div[@class='acResults'][1]//li[1]//span")).click();

driver.findElement(By.xpath("//input[@id='autocomplete_dest']"))
    .clear();

driver.findElement(By.xpath("//input[@id='autocomplete_dest']"))
    .sendKeys("Co");

driver.findElement(
    By.xpath("//div[@class='acResults'][2]//li[1]//span")).click();

回答by Mashekul Alam

Please use the code. it should work for Autocomplete for address forms.

请使用代码。它应该适用于地址表格的自动完成。

import java.util.List;

导入 java.util.List;

public class AutoSuggest {

公共类 AutoSuggest {

@Test(description="Auto Suggest")
public void selectValues() throws InterruptedException 
{
/*  System.setProperty("webdriver.gecko.driver", "geckodriver.exe");
    FirefoxDriver driver = new FirefoxDriver();
    */

System.setProperty("webdriver.chrome.driver","C:\Users\abcd\chromedriver.exe");

System.setProperty("webdriver.chrome.driver","C:\Users\abcd\chromedriver.exe");

    WebDriver driver = new ChromeDriver();
    //driver.get("http://www.google.com");


    driver.get("https://www.google.com/maps/search/maps+google/@37.5979349,-77.504561,15z/data=!3m1!4b1");
    driver.manage().window().maximize();
    driver.findElement(By.id("searchbox")).sendKeys("Glen Allen");
    Thread.sleep(2000);

    //cocRegion
Actions act = new Actions(driver);
    act.sendKeys(Keys.DOWN).perform();
    act.sendKeys(Keys.ENTER).perform();
    // This code for open a new Tab
    //act.keyDown(Keys.CONTROL).sendKeys("t").build().perform();

    /**
     * Example for Visibility of Elements located by
     */
    /*WebDriverWait wait = new WebDriverWait(driver,30);
    wait.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.id("locSelectVal")));

    List<WebElement> list = driver.findElements(By.id("locSelectVal"));

    System.out.println("Auto Suggest List ::" + list.size());

    for(int i = 1 ;i< list.size();i++)
    {
        System.out.println(list.get(i).getText());

        if(list.get(i).getText().equals("Glen Allen"))
        {
            list.get(i).click();
            break;


        }*/
    //driver.close();
    }



}

 import java.util.List;
 








import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.Test;
 
public class AutoSuggest {
 


 @Test(description="Auto Suggest")
 public void selectValues() throws InterruptedException 
 {
 /* System.setProperty("webdriver.gecko.driver", "geckodriver.exe");
  FirefoxDriver driver = new FirefoxDriver();
  */
System.setProperty("webdriver.chrome.driver","C:\Users\abcd\chromedriver.exe");
  
  WebDriver driver = new ChromeDriver();
  //driver.get("http://www.google.com");
  
  
  driver.get("https://www.google.com/maps/@37.5969128,-77.5061491,16.75z");
  driver.manage().window().maximize();
  driver.findElement(By.id("cocRegion")).sendKeys("Glen Allen");
  Thread.sleep(2000);
 
  //cocRegion
 Actions act = new Actions(driver);
  act.sendKeys(Keys.DOWN).perform();
  act.sendKeys(Keys.ENTER).perform();
  // This code for open a new Tab
  //act.keyDown(Keys.CONTROL).sendKeys("t").build().perform();
 
  /**
   * Example for Visibility of Elements located by
   */
  /*WebDriverWait wait = new WebDriverWait(driver,30);
  wait.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.id("locSelectVal")));
  
  List<WebElement> list = driver.findElements(By.id("locSelectVal"));
  
  System.out.println("Auto Suggest List ::" + list.size());
  
  for(int i = 1 ;i< list.size();i++)
  {
   System.out.println(list.get(i).getText());
   
   if(list.get(i).getText().equals("Glen Allen"))
   {
    list.get(i).click();
    break;
   
   
   }*/
  //driver.close();
  }
 

  
 }
 

enter image description here

在此处输入图片说明