Selenium C#如何选择以下文本输入区域:

时间:2017-11-09 11:32:44

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

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

<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>

我不需要提取任何文字。我需要进入该区域。

3 个答案:

答案 0 :(得分:0)

试试这段代码:

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

答案 1 :(得分:0)

尝试以下代码:

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");

答案 2 :(得分:0)

对于发送文本,您必须简单地使用sendkeys,java中的此代码请将其转换为c sharp

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

要提取文字,请使用getAttribute方法

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

你的xpath是正确的