无法将sendKeys()方法用于Selenium WebDriver的段落标记

时间:2016-01-26 13:26:08

标签: java html selenium selenium-webdriver

以下是我要编辑的网页图片:

enter image description here

这是HTML:

<div id="taTextElement7190829193028565" class="ng-pristine ng-untouched ng-valid ta-bind" contenteditable="true" ta-bind="ta-bind" ng-model="html">
<h3>Problem Scenario</h3>
<p>ff-224</p>
<h3>Diagnostics</h3>
<p>
<h3>Resolution Steps</h3>
<p>
</div>

现在,我正在使用sendKeys()函数将值传递给段落标记。

String PS_XPath = "//div[contains(@class, 'ng-pristine ng-untouched ng-valid ta-bind')]/p[2]";
WebElement element=driver.findElement(By.xpath(PS_XPath));
element.click();
element.sendKeys("Test");

sendKeys()功能无效。

1 个答案:

答案 0 :(得分:4)

仅使用普通的webdriver方法无法做到这一点。但是你可以使用JavascriptExecutor来做到这一点。 这应该有效:

JavascriptExecutor js = (JavascriptExecutor)driver; js.executeScript("document.getElementsByTagName('p')[2].innerHTML = 'Sample text'");

其中:

[2] - 所需标签的索引。

示例文字 - 替换为您的文字。