从搜索建议中选择或声明值

时间:2015-04-01 10:10:32

标签: java selenium

enter image description here我正在尝试使用selenium webdriver执行一些操作,如下所示

1.打开亚马逊网站 2.在搜索文本字段中键入ipad - 显示对文本的一些建议 3.执行操作,例如选择或打印建议中的项目数

当我这样做的时候,我观察到的是与DOM中没有出现的建议相关的HTML代码(纯粹的ajax响应),如果是这样的话我怎样才能执行任何操作,比如使用selenium webdriver随机选择项目。

另一点是,在输入ipad并点击go然后再次重新输入ipad将提供具有html代码的DOM以供建议。

但是我第一次输入时需要进行评估。

请提供您的建议。附件是截图

2 个答案:

答案 0 :(得分:0)

您可以通过检查它来始终拥有该元素。转到元素,单击在firepath中检查,它将为您提供xpath或css在那里所做的。虽然它不会为你提供隐藏的html。因此,在您的情况下,每个元素都附加一个唯一的ID。要获得所有元素:

d.findElement(By.id("twotabsearchtextbox")).sendKeys("iPhone");
List<WebElement> lst = d.findElements(By.xpath(".//*[contains(@id,'issDiv')]"));
for (WebElement webElement : lst) {
    System.out.println(webElement.getText());
}

按索引点击:

lst.get(4).click();

或基于文字,您可以通过以下方式完成:

List<WebElement> lst = d.findElements(By.xpath(".//*[contains(@id,'issDiv')]"));
for (WebElement webElement : lst) {
    if (webElement.getText().equalsIgnoreCase("your item to select"))
        webElement.click();
}

答案 1 :(得分:0)

是的确,自动搜索建议面板不能通过开发人员工具进行记录或识别。但我们仍然可以根据输入框中输入的前两个字符选择搜索建议。下面是我的自动化亚马逊java类中的一种方法有两种方法 - 一种是从下拉菜单中选择产品类型,另一种是选择搜索文本类型,比如&#34; Java&#34;并从自动搜索建议中选择第三个选项。

import org.openqa.selenium.firefox.*;
import org.openqa.selenium.interactions.*;
import org.openqa.selenium.support.ui.Select;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

public class AutomateAmazon1 {

    static WebDriver driver=new FirefoxDriver();
    Actions action=new Actions(driver);
    //method to select a given "book" product category from drop down
    public void selectAllorSingleProductAMZ()
    {
        action.moveToElement(driver.findElement(By.cssSelector("div.nav-search-scope.nav-sprite"))).build().perform();
        WebElement productsearchdd_nav=driver.findElement(By.cssSelector("select#searchDropdownBox"));
        Select productsearchdd=new Select(productsearchdd_nav);
        List<WebElement> productsearchddoptionsele=productsearchdd.getOptions();
        ArrayList<String> productsearchddoptionstext=new ArrayList<String>();
        for(WebElement we:productsearchddoptionsele)
        {
            productsearchddoptionstext.add(we.getText());
        }
        System.out.println(productsearchddoptionstext);

        for(String s:productsearchddoptionstext)
        {
            if(s.toString().contentEquals("Books"))
            {
                productsearchdd.selectByVisibleText("Books");
            }
        }
    }
    //method to input product search text and select third option from //auto search suggestions
    public void searchforSelectedPTAMZ()
    {
//input search text "ja" in search box      
driver.findElement(By.cssSelector("div.nav-search-field>input#twotabsearchtextbox.nav-input")).sendKeys("ja");
//move to first option on auto search suggestions using keyboard down //arrow key selection actions
        action.moveToElement(driver.findElement(By.cssSelector("div.nav-search-field>input#twotabsearchtextbox.nav-input"))).sendKeys(Keys.ARROW_DOWN).build().perform();
//move to second option on auto search suggestions using keyboard down //arrow key selection actions
        action.moveToElement(driver.findElement(By.cssSelector("div.nav-search-field>input#twotabsearchtextbox.nav-input"))).sendKeys(Keys.ARROW_DOWN).build().perform();
        action.moveToElement(driver.findElement(By.cssSelector("div.nav-search-field>input#twotabsearchtextbox.nav-input"))).sendKeys(Keys.ARROW_DOWN).build().perform();
//finally press enter to choose the selection from auto search suggestions //and input will be autofilled       
action.moveToElement(driver.findElement(By.cssSelector("div.nav-search-field>input#twotabsearchtextbox.nav-input"))).sendKeys(Keys.ENTER).build().perform();
    }

    public static void main(String[] args)
    {
        driver.get("http://www.amazon.in/");
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(60,TimeUnit.SECONDS);
        driver.manage().timeouts().pageLoadTimeout(60, TimeUnit.SECONDS);
        AutomateAmazon1 runscriptest1= new AutomateAmazon1();
        runscriptest1.selectAllorSingleProductAMZ();
        runscriptest1.searchforSelectedPTAMZ();

    }

}