Selenium从下拉列表中查找xpath以获取建议

时间:2016-10-27 12:14:55

标签: selenium xpath

伙计们,请你们帮助我。

我需要为iphone 6s找到xpath(当我在搜索字段中键入' iphone'时会给出建议)

enter image description here

这不起作用:

Actions actions = new Actions (driver);
actions.moveToElement(searchField).perform();
driver.findElement(By.linkText("iphone 6s")).click();

3 个答案:

答案 0 :(得分:0)

尝试执行以下操作:

Actions actions = new Actions (driver);
WebElement inputField = driver.findElement(By.xpath("//input[@type='text']"));
actions.moveToElement(inputField).sendKeys("iphone 6s")
.moveToElement(driver.findElement(By.xpath("//a[span[text()=' 6s']]")))
.click()
.build()
.perform();

答案 1 :(得分:0)

CSS选择器"a[data-query='iphone 6s']"应该有效。

driver.findElement(By.cssSelector("a[data-query='iphone 6s']")).click();

答案 2 :(得分:0)

您需要添加服务员才能显示建议,但由于结果不是By.linkText标记,您无法使用<a>

以下是该网站的工作示例(Java):

By searchInput = By.cssSelector("#gh-search-input");
By suggestionIPhone = By.cssSelector("ul.suggestion-list li[title='iphone 6s']");

driver.findElement(searchInput).sendKeys("iphone 6s");

new WebDriverWait(driver, 15)
  .until(ExpectedConditions.presenceOfElementLocated(suggestionIPhone))
  .click();