找不到网页元素,元素根据字段突出显示而更改

时间:2019-05-09 18:49:21

标签: selenium selenium-webdriver

找不到元素,尝试了几种方法,但无法到达元素

图片1:

id="1_s_1_l_MTO_Transaction_Type"Class=""(为空) 在图片中,突出显示的HTML(Document_For)是下一个字段,您可以看到Class被添加为class="edit-cell ui-state-highlight

enter image description here

图片2:

此图片中突出显示了所需字段,并id="1_s_1_l_MTO_Transaction_Type"Class="edit-cell ui-state-highlight"以及新的HTML被添加 <input id="1_MTO_Transaction_Type"...........

enter image description here

我尝试了以下操作:

driver.findElement(By.id("1_s_1_l_MTO_Transaction_Type")).click();

driver.findElement(By.xpath(("//tr[starts-with(@class,'ui-widget-content') and @role='row']//td[id='1_s_1_l_MTO_Transaction_Type']"))).click();

driver.findElement(By.xpath(("//tr[@id='1']/td[id='1_s_1_l_MTO_Transaction_Type']"))).click();

以上所有内容使我无法找到元素

图片3:

更多HTML可以找出元素的查找方式

这将是非常有用的帮助,因为我可以用相同的方式找到一系列元素。

enter image description here

2 个答案:

答案 0 :(得分:0)

映射列并单击索引(如我所见,当单击列单元格时,将创建输入字段,因此您必须先单击它)

尝试这样(这是C#,下面的Java):

List<IWebElement> TransactionTypeFields => driver.FindElements(By.CssSelector("td[id*='Transaction_type']"));

IWebElement TransactionTypeInput => driver.FindElement(By.CssSelector("td[id*='Transaction_type'] input"));

public void TypeInTransactionTypeCell(string value, int index)
{
    TransactionTypeFields[index].Click();
    TransactionTypeInput.SendKeys(value);
}

在Java中,使用您的逻辑,应该是这样的(使用记事本++可能会出现一些语法错误):

public void TypeInTransactionTypeCell(string value, int index)
{
    driver.findElements(By.cssSelector("td[id*='Transaction_type']")).get(index).click();
    driver.findElement(By.cssSelector("td[id*='Transaction_type'] input")).sendKeys(value);
}

让我知道它是否有效!

答案 1 :(得分:0)

使用ID选择交易类型,然后按照以下代码进行操作。

// Select Transaction Type 
String Transaction_Type = ExcelUtils.getCellData(8, 2); 
driver.findElement(By.xpath(("//td[contains(@id,'Transaction_Type')]"))).click(); 
driver.findElement(By.id("s_1_2_47_0_icon")).click(); 
driver.findElement(By.xpath("//li[@class='ui-menu-item']/div[contains(text(), '" + Transaction_Type + "')]")).click();