当我运行以下程序时,它运行得非常好,直到我开始填写 webclip 部分的表格。到达那里后,它说找不到 ID“txtTemplateName”。我直接从检查元素中复制并粘贴了该名称。也不是我没有给它足够的时间来加载,我过去给了它整整 5 秒只是为了确定,但仍然没有。
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using System.Diagnostics;
using System.Linq;
namespace Build1Evan
{
class Program
{
static void Main(string[] args)
{
IWebDriver driver1 = new FirefoxDriver();
driver1.Navigate().GoToUrl("URL");
Process.Start(@FILE"); // This is just an Auto IT Script to get me by a login window
bool loginCheck1 = driver1.FindElements(By.Id("btnLoginAgain")).Count() > 0;
if (loginCheck1 == true)
{
driver1.FindElement(By.Id("btnLoginAgain")).Click();
}
System.Threading.Thread.Sleep(500);
driver1.FindElement(By.LinkText("Configuration")).Click();
System.Threading.Thread.Sleep(500);
driver1.FindElement(By.LinkText("Payloads")).Click();
System.Threading.Thread.Sleep(500);
driver1.FindElement(By.PartialLinkText("WebClip")).Click();
// BELOW IS WHERE I ENCOUNTER THE ISSUE
System.Threading.Thread.Sleep(500);
driver1.FindElement(By.Id("txtTemplateName")).SendKeys("TESTING");
}
}
}
当我尝试选择表单中的任何其他元素(而不是按钮或其他文本字段)时,我也会遇到错误。
编辑:PHOTO根据要求
编辑 2:PHOTO 2仅供引用,我在使用 Selenium IDE 时没有遇到任何问题,它很容易选择元素并填充它。
最佳答案
根据您的屏幕截图,我认为您的应用程序使用 <object id="panelcontentobject" data="...
嵌入了表单.
如果表单真的在对象标签内,您必须创建对象标签的 WebElement,然后使用 JavaScriptExecutor 检索属性或与表单交互。 例如,
IWebElement objectTag= driver.FindElement(By.Id("panelcontentobject"));
IJavaScriptExecutor js = driver as IJavaScriptExecutor;
js.ExecuteScript("return (arguments[0].contentDocument.getElementById('txtTemplateName')).click()",objectTag);
请参阅 http://aksahu.blogspot.com/2015/05/dealing-with-object-tags-in-selenium-webdriver.html?m=1 中的示例
关于c# - Selenium 告诉我 ID 不存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34797832/