【问题标题】:Selenium C# How to select following text input area:Selenium C# 如何选择以下文本输入区域:
【发布时间】:2018-04-22 09:12:51
【问题描述】:

我不确定如何选择下面编码的输入区域:

<input _ngcontent-c15="" class="form-control form-control-danger ng-pristine ng-invalid ng-touched" placeholder="Name of the snapshot definition " type="text" ng-reflect-form="[object Object]">

此外,在文本值更改后,上述更改如下:

 <input _ngcontent-c15="" class="form-control form-control-danger ng-touched ng-dirty ng-valid" placeholder="Name of the snapshot definition " type="text" ng-reflect-form="[object Object]">

下同:

<input _ngcontent-c15="" class="form-control form-control-danger ng-pristine ng-valid ng-touched" placeholder="Description snapshot definition" type="text" ng-reflect-form="[object Object]">

更改为:

<input _ngcontent-c15="" class="form-control form-control-danger ng-valid ng-touched ng-dirty" placeholder="Description snapshot definition" type="text" ng-reflect-form="[object Object]">

我尝试了以下多种组合:

element = driver.FindElementByXPath("//input[@class='form-control form-control-danger ng-pristine ng-invalid ng-touched']");

element = driver.FindElementByXPath("//input[contains(@class,'form-control form-control-danger ng-pristine ng-invalid ng-touched')]");

element = driver.FindElementByXPath("//*[contains(@class,'form-control form-control-danger ng-pristine ng-invalid ng-touched')]");

element = driver.FindElementByXPath("//a[contains(@class,'form-control form-control-danger ng-pristine ng-invalid ng-touched')]");

element = driver.FindElementByCssSelector("placeholder='Name of the snapshot definition '");

相关区域的完整 div:

<div _ngcontent-c25="" class="form-group has-danger" ng-reflect-klass="form-group" ng-reflect-ng-class="[object Object]">
<label _ngcontent-c25="">Name</label>
<input _ngcontent-c25="" class="form-control form-control-danger ng-pristine ng-invalid ng-touched" placeholder="Name of the snapshot definition " type="text" ng-reflect-form="[object Object]">
<!--bindings={"ng-reflect-ng-if": "true"}-->
<div _ngcontent-c25="" class="alert alert-danger">Required Field</div>
<!--bindings={"ng-reflect-ng-if": "false"}-->
<!--bindings={"ng-reflect-ng-if": "false"}-->
</div>

我不需要提取任何文本。我需要将其输入该区域。

【问题讨论】:

  • 你要选中的文字
  • 抱歉,我想选择发送文本的区域。
  • 你试试按名称查找
  • 将使用什么名称?这些文本字段是动态添加的,因此在我重新加载页面后,_ngcontent-c15 现在是 _ngcontent-c19。
  • 名字在哪里??

标签: c# selenium xpath css-selectors selenium-chromedriver


【解决方案1】:

试试下面的代码:

element = driver.FindElement(By.xpath("//input[@class='form-control form-control-danger ng-pristine ng-invalid ng-touched']"));

string value = element.GetAttribute("placeholder"); 

如果你想发送文本然后

element.SendKeys("Text here");

如果您想对该字段进行更具体的说明,请在 xpath 下方使用

 driver.FindElement(By.xpath("//label[text()='Name']/following-sibling::input")).SendKeys("username");

【讨论】:

  • 嗨!我在您发布的driver.FindElement(By.xpath("//label[text()='Name']/following-sibling::input")).SendKeys("username"); 上取得了一些成功,但是在尝试.sendkeys() 时不再找到该元素。
  • new WebDriverWait(driver, TimeSpan.FromSeconds(10)).Until(ExpectedConditions.ElementExists(By.XPath("//label[text()='Name']/following-sibling::input"))); element = driver.FindElement(By.XPath("//label[text()='Name']/following-sibling::input")); highlightElement(driver, element); 我有这个运行良好的代码。只有当我想SendKeys() 我现在有“不可见”的问题。
  • 试试这个new WebDriverWait(driver, TimeSpan.FromSeconds(60)).Until(ExpectedConditions.ElementIsVisible(By.XPath("//lab‌​el[text()='Name']/fo‌​llowing-sibling::inp‌​ut"))); element = driver.FindElement(By.XPath("//label[text()='Name']/followin‌​g-sibling::input")); element.SendKeys(text); highlightElement(driver, element);
  • 没有。尽管页面如下所示:i.gyazo.com/eb7998b5de66e856b4e0b690dc73ab7d.png Selenium 不会找到“可见”元素,但可以看到它“存在”。
  • 我设法用 JavaScript 做到了:public void enterText(IWebDriver driver, IWebElement element, String Text) { IJavaScriptExecutor js = (IJavaScriptExecutor)driver; js.ExecuteScript("arguments[0].value='" + Text + "'", element); } 谢谢你的帮助。
【解决方案2】:

发送文本你必须简单地使用sendkeys,这个java中的代码请把它转换成cSharp

driver.findElement(By.xpath("your values")).sendKeys("test");

提取文本使用getAttribute方法

String Placehodlertxt= driver.findElement(By.xpath("your values")).getAttribute("placeholder"); 
System.out.println(Placehodlertxt);

你的 xpath 是正确的

【讨论】:

  • XPath 未找到。我只需要能够访问 区域,这样我就可以首先将其传递给:public void highlightElement(IWebDriver driver, IWebElement element) { IJavaScriptExecutor js = (IJavaScriptExecutor)driver; js.ExecuteScript("arguments[0].style.border='3px solid red'", element); },然后将文本发送给它。
  • 是的,你可以使用 javascriptExecutpr,在 c 中也可以使用 IJavaScriptExecutor
【解决方案3】:

试试这个代码:

IWebElement element = driver.FindElement(By.xpath("//input[@placeholder='Name of the snapshot definition']")).getAttribute("placeholder"); 

【讨论】:

  • 感谢您的回复。不幸的是,这会返回“无法将类型字符串隐式转换为 OpenQA.Selenium.IWebElement”,并且删除“GetAttribute”会导致等待元素超时。
  • 您要在哪个对象中存储数据
  • :&lt;input _ngcontent-c25="" class="form-control form-control-danger ng-pristine ng-invalid ng-touched" placeholder="Name of the snapshot definition " type="text" ng-reflect-form="[object Object]"&gt;
猜你喜欢
  • 1970-01-01
  • 2011-06-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-16
  • 2014-10-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多