如何通过C#和Selenium向<p>标签发按键?及textarea发内容失败求助
Hey there! Let's tackle your two Selenium + C# issues one by one:
<p> Tag with C# and Selenium First off, <p> tags aren't natively editable unless they have the contenteditable="true" attribute. Here's how to handle both scenarios:
If the <p> is already editable
Start by waiting for the element to be interactive, activate it with a click, then send your keys:
// Wait for the editable <p> to become visible and interactable var editableParagraph = new WebDriverWait(driver, TimeSpan.FromSeconds(10)) .Until(ExpectedConditions.ElementIsVisible(By.CssSelector("p[contenteditable='true']"))); // Click to focus the element (critical for sending keys) editableParagraph.Click(); // Send regular text editableParagraph.SendKeys("Your desired text here"); // Send special keys like Enter or Backspace editableParagraph.SendKeys(Keys.Enter);
If the <p> is not editable (needs to be enabled first)
Use JavaScript to add the contenteditable attribute before interacting:
var targetParagraph = driver.FindElement(By.CssSelector("p.your-paragraph-class")); IJavaScriptExecutor js = (IJavaScriptExecutor)driver; // Make the paragraph editable js.ExecuteScript("arguments[0].setAttribute('contenteditable', 'true');", targetParagraph); // Now focus and send keys as before targetParagraph.Click(); targetParagraph.SendKeys("Editable text added!");
The HTML you shared is for a TextAngular (Angular-based) rich editor—this isn't a native <textarea>, so you can't interact with it like a standard input. Instead, target the editor's internal editable region:
Method 1: Using SendKeys (simulates user input)
// Wait for the editor's editable content area to load var editorContent = new WebDriverWait(driver, TimeSpan.FromSeconds(10)) .Until(ExpectedConditions.ElementIsVisible(By.CssSelector(".ta-editor [contenteditable='true']"))); // Click to activate the editor editorContent.Click(); // Clear any existing content (optional but useful) editorContent.SendKeys(Keys.Control + "a"); editorContent.SendKeys(Keys.Delete); // Send your "description" text editorContent.SendKeys("description");
Method 2: Using JavaScript (more reliable for rich editors)
If SendKeys doesn't work due to editor quirks, directly set the content via JS:
IJavaScriptExecutor js = (IJavaScriptExecutor)driver; var editorWrapper = driver.FindElement(By.CssSelector(".ta-editor")); // Set the inner HTML of the editable region to your text js.ExecuteScript("arguments[0].querySelector('[contenteditable=true]').innerHTML = 'description';", editorWrapper); // Bonus: If the editor is bound to an Angular ng-model, update the model directly (ensure you know the model name) // js.ExecuteScript("angular.element(arguments[0]).scope().yourModelVariable = 'description'; angular.element(arguments[0]).scope().$apply();", editorWrapper);
The key here is recognizing that rich editors like TextAngular use editable <div>/<p> elements under the hood instead of a traditional <textarea>.
内容的提问来源于stack exchange,提问作者Hasan Sarıkaya

