【问题标题】:How to send text within a textarea having the attribute style="display: none;" through C# and Selenium如何在具有属性 style="display: none;" 的文本区域内发送文本通过 C# 和 Selenium
【发布时间】:2023-03-29 20:49:02
【问题描述】:

我需要使用 selenium 中的简单 sendkeys 函数在 textarea 中写入。 这个 textarea (ID = 'txtSkillsTaught-Value') 后跟一个脚本标签,我猜想我无法在其中隐藏 textarea 的可见性。

尝试了简单的 driver.findelment(By.Id("txtSkillsTaught-Value")).sendkeys("text"); 甚至尝试切换到上面的 iframe 但没有奏效 附上HTML代码图片

谢谢, 阿美

【问题讨论】:

  • 在这里以文本格式分享同一张图片。你得到的错误是什么?
  • 您可以手动输入文字吗?使文本框可见的步骤是什么?
  • 是的,我可以手动输入文本
  • @Amey :您如何手动将内容发送到文本框?同样可以自动化。

标签: javascript c# selenium selenium-webdriver webdriverwait


【解决方案1】:

一方面,如果它不可见,将文本放入其中可能不是一个好主意......但是,另一方面,我有时需要制作这样的东西。我通常在我的 selenium 代码中使用 javascript 执行发送密钥之前更改此元素的可见性(我使用 java,但对于 C# 应该或多或少相同):

executeScript("$('.yui-button.yui-link-button').find(\"textarea[id='txtSkillsTaught-Value']\").css({'opacity':'1', 'visibility':'visible', 'display':'block', 'position':'relative', 'transform':'none'})");
driver.findelment(By.Id("txtSkillsTaught-Value")).sendkeys("text");

应该可以的。

【讨论】:

    【解决方案2】:

    根据您提供的 HTML<textarea> 不在 <iframe> 范围内,但具有 属性 style="display: none;" 。因此,要将字符序列发送到<textarea>,您可以使用以下解决方案:

    IWebElement element = new WebDriverWait(driver, TimeSpan.FromSeconds(10)).Until(ExpectedConditions.ElementToBeClickable(By.XPath("//td[@class='t-editable-area']//textarea[@class='t-content t-raw-content' and @id='txtSkillsTaught-value']")));
    ((IJavaScriptExecutor)driver).ExecuteScript("return arguments[0].removeAttribute('style')", element);
    element.SendKeys("Amey");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-31
      • 2019-05-08
      • 1970-01-01
      • 2013-04-13
      • 2015-09-18
      • 2013-07-01
      相关资源
      最近更新 更多