2013-04-03 48 views
0

我目前正在測試我的應用程序的GUI,我想知道是否有可能將焦點設置爲WebElement?關注WebElement?

我使用Selenium 2.0和webdriver。

所以我在尋找類似的東西:driver.findElement(xxxx).setfocus();

謝謝。

編輯: 我alreardy測試之類的技巧性的東西

// getting the element WebElement 
eSupplierSuggest = driver.findElement(By.xpath("...")); 

//get le location and click 
Point location = eSupplierSuggest.getLocation(); 
new Actions(driver).moveToElement(eSupplierSuggest, location.x, location.y).click(); 

//or 

//directly perform 
new Actions(driver).moveToElement(eSupplierSuggest).click().perform(); 

我紅色的地方,點擊焦點的元素,但對我來說,沒有什麼工作。笑

PS:這是原帖的子問題Click on a suggestbox, webdriver

+0

使用任何框架?或只是HTML CSS JS應用程序 – daemonThread

+0

@daemon:是的,我使用Selenium 2,我會在問題上添加它。我的錯。 – e1che

+0

你能告訴我,你爲什麼需要關注嗎?它是否類似於您正在測試的自我暗示盒? – Hemanth

回答

0

嘗試如下所示使用cssSelector的自我暗示的點擊,讓我知道,如果你仍然面臨的問題。

// supplier ops, i find and type data into the input 
    WebElement eSupplier = driver.findElement(By.id("supplier:supplierOps_input")); 
    eSupplier.sendKeys("OPS1"); 
    sleep(5); // wait the suggestbox 

    // i find the suggestbox 
    WebElement eSupplierSuggest = driver.findElement(By.cssSelector("css path of that specific value in the auto suggestion box")); 
    eSupplierSuggest.click(); 
    sleep(5); // wait the refresh for the next field supplierAddress 
0

WebDriver API沒有功能來設置元素的焦點。

如果你想這樣做,你將不得不編寫一些JavaScript來設置焦點,然後使用JavaScriptExecutor來運行JavaScript。

0

確保,你沒有改變幀.... 其他明智的。點擊()應該做的伎倆

3

我常因此被集中發送一個空的關鍵要素。因此,例如:

element.send_keys "" 
1

爲了設置焦點可以使用executeScript方法在元件上,如下所述:

JavascriptExecutor js; 
js.executeScript ("document.getElementById('x').focus()"); 

一旦焦點被設置就可以很容易地使用由webdriver的API提供send_keys。

+0

明天我會試試,謝謝=) – e1che