无法单击另一个元素中的正确元素



我试图按行的名称获取某个行,然后单击该元素中的某个元素。我所做的是:

// searching for a row with the given name of the column content
WebElement row = driver.findElement(By.xpath("//div[contains(text(), 'John Smith']//ancestor::div[@class='row']"));
//clicking button within the row
Webelement button = row.findElement(By.xpath("//button[@class='Click']"))

它不是这样工作的,它总是单击表中第一行的按钮。P.S.WebElement row本身被正确识别,并且它找到了正确的行。只有当我想搜索所需行中的按钮时。对此有什么建议吗?

要访问父元素的直属子元素,需要提供.

Webelement button = row.findElement(By.xpath(".//button[@class='Click']"))

或者你可以使用这个

Webelement button = driver.findElement(By.xpath("//div[//div[contains(text(), 'John Smith']]/button[@class='Click']"))

两者都应该有效。

您试图使用elem而不是来查找按钮。也许使用row.findElement(By.xpath("//button[@class='Click']&"((可以解决您的问题。

最新更新