用java将selenium中的文本粘贴到只读输入中

zbsbpyhn  于 10个月前  发布在  Java
关注(0)|答案(1)|浏览(129)

我有输入div,如果你点击它一些方法添加一些属性,以防止用户添加文本与键盘和使用选择轮!在java selenium中,我可以获取元素移除readonly属性,但如果使用sendKeys,则不会发生任何事情,因为键盘输入关闭(可能通过JavaScript函数或通过导航器锁),如果右键单击并粘贴一些内容工作这很好地知道,如果重新选择输入的只读和所有类回它从JavaScript所以我可以做什么来粘贴文本从剪贴板没有右键单击或使用(clt+V或shift + insert)。
div看起来:

<ui-text-field _ngcontent-ehh-c219="" label="تاریخ انقضا" name="expirationDate" _nghost-ehh-c120="">
   <div _ngcontent-ehh-c120="" class="text-field is-invalid is-readonly">
      <form _ngcontent-ehh-c120="" novalidate="" class="ng-pristine ng-invalid ng-touched">
         <!----><input _ngcontent-ehh-c120="" formcontrolname="field" class="field ng-pristine ng-invalid ng-star-inserted ng-touched" type="text"><!----><!----><!----><!----><!----><!----><!----><!----><label _ngcontent-ehh-c120="">تاریخ انقضا</label>
      </form>
      <!---->
   </div>
</ui-text-field>

字符串
直接和向前说话是:如何可以粘贴文本内容到div没有右键单击/行动预制或键盘与 selenium 在java中。

ffdz8vbo

ffdz8vbo1#

使用JavascriptExecutor接口提供的executeScript方法
使用方法,可以执行直接修改输入字段值的JavaScript代码段
请参阅下面使用Selenium Java的框架代码:

import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

WebElement inputElement = driver.findElement(By.cssSelector("input.field"));  // replace with your selector / value

// cast the driver to JavascriptExecutor
JavascriptExecutor js = (JavascriptExecutor) driver;

// remove the 'is-readonly' class using JavaScript
js.executeScript("arguments[0].classList.remove('is-readonly')", inputElement);

// add a new class if required using JavaScript
js.executeScript("arguments[0].classList.add('new-class')", inputElement);

// add a new value/attribute if required using JavaScript
js.executeScript("arguments[0].value = 'Your desired value';", inputElement);

字符串

*注意: 如果网页重新渲染或触发某些JavaScript事件,可能会删除任何更改 *

相关问题