无法在表单类或div类中找到元素

时间:2018-05-04 10:36:38

标签: selenium automated-tests

我无法在表单类或div类中找到元素:

//Tried this clicking on Source name.
driver.findElement(By.id("__BVID__62")).click();
//Typing into Source name.
driver.findElement(By.id("__BVID__63")).sendKeys(new String[]{"CNN"});

我试过了:

driver.findElement(By.xpath("//input[contains(@class='form-control') and type='text']")).sendKeys("CNN");

HTML code:

<div class="card-body">
<fieldset id="_BVID__62" role="group" aria-labelledby="__BVID__62__BV_label" class="b-form-group form-group">
<legend id="_BVID__62__BV_label" class="col-form-label pt-0">Source Name</legend>
<div role="group" aria-labelledby="_BVID__62__BV_label" class="">
<input id="__BVID__63" type="text" class="form-control">

2 个答案:

答案 0 :(得分:0)

使用此代码:

WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement input = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@class='card-body']/descendant::div/input[contains(@id,'BVID')]")));
input.sendKeys("CNN");

答案 1 :(得分:0)

根据您共享的HTML,所需元素具有动态ID。因此,要将文本发送到<input>节点,您必须按如下方式引导 WebDriverWait

new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@class='form-control' and starts-with(@id,'__BVID__') and @type='text']"))).sendKeys("CNN");