如何处理硒中的自动建议

时间:2014-05-26 05:43:27

标签: java selenium selenium-webdriver

//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();

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

3 个答案:

答案 0 :(得分:2)

提供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");

请提供网址或HTML,以便我们可以更多地了解它。

以下代码适用于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();

//点击提交

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

答案 1 :(得分:0)

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

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();

答案 2 :(得分:0)

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

import java.util.List;

公共类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/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