我想從 ql-editor 中取出字串。
下面的代碼也適用于其他文本欄位,如電子郵件/密碼文本欄位等。但是在 ql 編輯器中它不起作用。變數checkText接收 Null。
public static void SendKeysElement(IWebDriver webDriver, string statusMessage)
{
IWebElement Field = webDriver.FindElement(By.XPath("//div[@class='ql-editor']//p"));
Field.SendKeys("Do this and this");
Thread.Sleep(500);
string checkText = Field.GetAttribute("value");
if (maxTries < 0)
{
throw new Exception("String not found");
}
else if (checkText != "Do this and this")
{
Thread.Sleep(1000);
maxTries--;
SendKeysElement(webDriver, statusMessage);
}
Console.WriteLine(statusMessage);
}
這是檢查:

uj5u.com熱心網友回復:
您的p元素(包含輸入的文本)沒有value屬性,因此您需要這樣做:
string checkText = Field.Text;
uj5u.com熱心網友回復:
您正在嘗試檢索valueP 標簽的 。value與實際上不包含標簽內的文本的 INPUT 等一起使用。在這種情況下,標簽是 P 所以使用
string checkText = Field.Text;
話雖如此,你在你的方法上做得太多了。名稱是,SendKeysElement()但您不僅對定位器進行了硬編碼,還對隨驗證發送的文本進行了硬編碼。您應該將每個主要操作分解為單獨的代碼段。使其SendKeysElement()足夠通用,可用于將文本放入任何元素并添加等待。
public static void SendKeysElement(IWebDriver webDriver, By locator, string text)
{
new WebDriverWait(webDriver, TimeSpan.FromSeconds(10)).Until(ExpectedConditions.ElementIsVisible(locator)).SendKeys(text);
}
創建另一種方法來從元素獲取文本(并添加一個等待)并使其足夠通用以使其可重用。
public static string GetText(IWebDriver webDriver, By locator)
{
return new WebDriverWait(webDriver, TimeSpan.FromSeconds(10)).Until(ExpectedConditions.ElementIsVisible(locator)).Text;
}
現在在你的測驗中,它應該看起來像
string text = "Do this and this";
By qlEditorLocator = By.XPath("//div[@class='ql-editor']//p");
SendKeysElement(webDriver, qlEditorLocator, text); ;
Assert.AreEqual(text, GetText(webDriver, qlEditorLocator), "Verify ql Editor string");
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/315543.html
