2017-01-10 54 views
2

我有這個outerHtml元素 -硒不能發送鍵中找到的元素

<input class="inp-text ps-component ng-valid-maxlength ng-touched ng-pristine ng-empty ng-invalid ng-invalid-required" ng-model-options="{ updateOn: 'default blur', debounce: { 'default': 200, 'blur': 0 } }" required="required" maxlength="100" ng-model="model.name"/> 

我曾試圖 -

driver.findElement(By.cssSelector("input")); 

可以找到這個元素,但是當我嘗試做

java的告訴我,element is not visible例外

driver.findElement(By.cssSelector("input")).click(); 

不起作用。

如何在此輸入元素中發送一些鍵?

我找到了解決方案的一部分。點擊()如果使用它的工作:

WebElement input = driver.findElement(By.cssSelector("input")) 
JavascriptExecutor js = (JavascriptExecutor)driver; 
js.executeScript("arguments[0].click();", input); 

但我仍然不明白如何發送密鑰。

+0

檢查我的答案。它包含JavascripExecutor'sendKeys()'。 – JDelorean

+0

你能發佈完整的錯誤信息嗎? – Guy

回答

2

嘗試使用顯式的等待與Expected Conditions,以確保該元素是可見它

WebDriverWait wait = new WebDriverWait(driver, 10); 
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("input"))); 
element.sendKeys("something"); 
+0

我說findElement()的作品。所以它不是隱形的。我不需要等待。 –

+0

@BerasMark如果'findElement()'起作用,則意味着DOM中的元素*存在*。這並不意味着它*可見*。您甚至可以在錯誤消息「java告訴我該元素不可見」中看到它。 – Guy

+0

對不起,我的錯誤解釋。 –

1

交互收到了其中的元素不是到WebDriver可見同樣的問題。相反,JavascriptExecutor能夠幫助我。下面是一些簡單的應用:

sendKeys()與JavascriptExecutor:

// Cast driver to JavascriptExecutor 
JavascriptExecutor jse = (JavascriptExecutor) driver; 
jse.executeScript("element = document.getElementsByTagName('input');"); 
jse.executeScript("element(0).value='" + yourInputString + "';"); 

click()與JavascriptExecutor:

// Cast driver to JavascriptExecutor 
JavascriptExecutor jse = (JavascriptExecutor) driver; 
jse.executeScript("javascript:submitForm()"); 
+0

可能是你的意思是元素[0]而不是元素(0)?無論如何,它仍然不會以我的輸入形式發送密鑰 –

+0

它是'element(0)'。請注意,您正在獲取標記爲'input'('getElements ...')的所有元素。您需要確保您的輸入字段是這些元素中的第一個,或者您要將輸入發送到列表中的正確元素。 – JDelorean

0

看來你正在嘗試使用的標籤名稱即<input> 這個標籤可以被找到在這種情況下,在同一頁中有多個。

driver.findElement(By.cssSelector("input")); 

不會顯示任何錯誤。 只是讓你的定位更具體的,然後嘗試e.g.-

driver.findElement(By.cssSelector("input.ps-component.ng-valid-maxlength.ng-touched.ng-pristine.ng-empty.ng-invalid.ng-invalid-required")).sendKeys("your_Value"); 

,或者你可以根據你的頁面的唯一loacate你的元素。

0

它的工作原理

js.executeScript("arguments[0].value=\"" + myText + "\";", input);