簡體   English   中英

無法讀取未定義錯誤的屬性“ setValue”。 如何使用Selenium WebDriver和Java在codeMirror元素中鍵入文本?

[英]Cannot read property 'setValue' of undefined error. How to type text in codeMirror element using Selenium WebDriver and Java?

我正在嘗試在textarea中輸入文本。 據我所知,該字段是CodeMirror。 這是我的代碼:

{...

WebElement scriptField = 

this.getDriver().findElement(By.cssSelector(".CodeMirror-line>span"));

JavascriptExecutor js = (JavascriptExecutor) this.getDriver();

String query = "text";

js.executeScript("arguments[0].CodeMirror.setValue(\""+ query +"\");", scriptField);

}

我在代碼的最后一行有此錯誤:

org.openqa.selenium.WebDriverException : unknown error: Cannot read property 'setValue' of undefined

我不確定在scriptField變量中應該引用哪個webelement。 當我手動鍵入文本時,它會出現在以下元素中: ".CodeMirror-line>span" 那我正確嗎?

所附圖片上有我的DOM片段。

在此處輸入圖片說明

一旦有了WebElement(在這種情況下為scriptField),只需使用sendKeys()方法。

WebElement scriptField = this.getDriver().findElement(By.cssSelector(".CodeMirror-line>span"));
scriptField.sendKeys("your text");

您還可以在添加文本之前清除該字段。

WebElement scriptField = this.getDriver().findElement(By.cssSelector(".CodeMirror-line>span"));
scriptField.clear();
scriptField.sendKeys("your text");

如果要在span元素內設置實際文本,請嘗試以下操作:

js.executeScript("arguments[0].innerText = " + query + ";", scriptField);

要將現有文本更改為myText使用以下代碼塊:

WebElement scriptField = this.getDriver().findElement(By.cssSelector("pre.CodeMirror-line>span"));
JavascriptExecutor jse = (JavascriptExecutor) this.getDriver();
String js = "arguments[0].innerHTML='myText';";
jse.executeScript(js, scriptField);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM