2016-10-27 21 views
0

夥計們,請你幫幫我。Selenium從下拉列表中找到xpath建議

我需要找到的XPath爲iPhone 6S(當我鍵入searchfield 'iphone' 的建議給出)

enter image description here

這不起作用:

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

鏈接文本不是「iPhone 6S」嘗試全文或使用數據查詢屬性? – user993553

+0

'Byxxpath(「// * [contains(text(),'iphone 6s'))]''? – JDelorean

+0

我試過了。沒有工作 – Aksana

回答

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

nope。不起作用 – Aksana

+0

任何異常? – Andersson

+0

沒有這樣的元素:無法找到元素:{「method」:「xpath」,「selector」:「// a [span [text()='6s']]」} – Aksana

0

CSS選擇器"a[data-query='iphone 6s']"應該工作。

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

不起作用。no such element:Unable to locate element :{「method」:「css selector」,「selector」:「a [data-query ='iphone 6s']」} – Aksana

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(); 
相關問題