【问题标题】:Manipulating text values using Selenium JavascriptExecutor使用 Selenium JavascriptExecutor 操作文本值
【发布时间】:2023-02-23 23:17:18
【问题描述】:

我正在尝试使用 Selenium 更改字段的文本值。不幸的是,该字段不是 <input>,而是一个伪类为 ::before 的 div

我需要能够在此字段中输入新的文本值,但到目前为止还无法这样做。

HTML sn-p:

<div class="stb-LazyChosenDropdown" tabindex="0">
   <div class="icon default" aria-hidden="true" style="display: none;"></div>
   <div class="tiles">
      <div class="input" contenteditable="true" placeholder="Enter values..." style="width: 383px; height: 17px;"></div>
        ::before
   </div>

我可以使用以下内容打印已有的值:

 JavascriptExecutor js = (JavascriptExecutor)driver;
 js.executeScript("return window.getComputedStyle(document.querySelector('div.stb-LazyChosenDropdown div.tiles div.input'),':before').getPropertyValue('content')")
 System.out.println(content);

打印Enter values...

但是如何将文本值更改为新值呢?

【问题讨论】:

    标签: javascript selenium-webdriver


    【解决方案1】:

    使用任何定位器策略查找元素并尝试添加 textContent

    JavascriptExecutor js = (JavascriptExecutor)driver;
    js.executeScript("arguments[0].textContent='Steerpike'", driver.findElement(By.cssSelector("div.input[placeholder^='Enter values']")))
    

    【讨论】:

      猜你喜欢
      • 2020-06-09
      • 2015-04-19
      • 2021-12-21
      • 1970-01-01
      • 2021-01-10
      • 1970-01-01
      • 2019-08-28
      • 2019-03-25
      • 1970-01-01
      相关资源
      最近更新 更多