无法使用Selenium Web驱动程序C#在文本区域中输入文本



以下是源代码。

我想在下面的文本框区域中输入文本。无法输入数据。请告诉我,我如何在下面的文本框中输入文本。

<td id="ctl00_cRight_ucSMS_redSMSBodyCenter" class="reContentCell" style="height:100%;">
<label for="ctl00_cRight_ucSMS_redSMSBodyContentHiddenTextarea" style="display:none;">
RadEditor hidden textarea
</label>
<textarea id="ctl00_cRight_ucSMS_redSMSBodyContentHiddenTextarea" name="ctl00$cRight$ucSMS$redSMSBody" rows="4" cols="20" style="display:none;">
</textarea>
<iframe frameborder="0" src="javascript:'<html></html>';id="ctl00_cRight_ucSMS_redSMSBody_contentIframe" title="Rich text editor with ID ctl00_cRight_ucSMS_redSMSBody" style="width: 100%; height: 218.009px; margin: 0px; padding: 0px;">
</iframe></td>

我已输入以下代码以输入数据。

IWebElement userid = FamosDriver.WebDriver.FindElement(By.Id("ctl00_cRight_ucSMS_redSMSBodyCenter"));
userid.SendKeys("Test");

此外,我还尝试了以下Java脚本执行器代码。

IJavaScriptExecutor jst = FamosDriver.WebDriver as IJavaScriptExecutor;
jst.executeScript("document.getElementById('ctl00_cRight_ucSMS_redSMSBodyCenter').value='testuser'"); 

我是不是错过了什么?我不知道如何修复这个

<textarea id="ctl00_cRight_ucSMS_redSMSBodyContentHiddenTextarea" name="ctl00$cRight$ucSMS$redSMSBody" rows="4" cols="20" style="display:none;">
</textarea>

如果您查看样式属性,它会将style="display:none;显示为none。

您需要更改textarea的属性,然后使用send_keys()使用java脚本执行器来设置属性。

IWebElement userid = FamosDriver.WebDriver.FindElement(By.Id("ctl00_cRight_ucSMS_redSMSBodyCenter"));
IJavaScriptExecutor js =FamosDriver.WebDriver as IJavaScriptExecutor;;
js.executeScript("arguments[0].style='display: block;'", userid);
userid.SendKeys("Test");

您已经在框架中了,我可以从您的HTML代码中看到这一点。这意味着您只需要切换到ifreame执行一些操作并切换回默认内容。有多种方法可以实现它。对于C#来说,它看起来像:

//Use one of these 4 options
driver.SwitchTo().Frame(frame-id);
driver.SwitchTo().Frame("frame-name");

/* weblocator can be XPath, CssSelector, Id, Name, etc. */
driver.SwitchTo().Frame(driver.FindElement(By.weblocator("web-locator-property")));
//////YOUR ACTION
driver.SwitchTo().DefaultContent();

在上查看更多信息https://www.lambdatest.com/blog/handling-frames-and-iframes-selenium-c-sharp/

最新更新