英文:
cannot click proper element within another element
问题
我正在尝试通过名称获取某一行,然后单击该元素中的一个元素。
我所做的是:
// 搜索具有给定列内容名称的行
WebElement row = driver.findElement(By.xpath("//div[contains(text(), 'John Smith')]/ancestor::div[@class='row']"));
// 单击行内的按钮
WebElement button = row.findElement(By.xpath("//button[@class='Click']"))
以这种方式不起作用,它总是单击表中的第一行中的按钮。附言:WebElement row
本身被正确识别,它找到了正确的行。只有当我想在所需的行内搜索按钮时才有问题。有关此问题的任何建议吗?
英文:
I'm trying to get some row by its name and then click on an element within that element.
What I do is:
// 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']"))
It is not working that way, it always clicks the button in the first row in the table. P.S. The WebElement row
itself is properly identified and it finds the correct row. Only when I want to search a button within desired row. Any suggestions on that?
答案1
得分: 0
你正在尝试使用elem而不是row来查找按钮。
也许使用row.findElement(By.xpath("//button[@class='Click']"))会解决你的问题。
英文:
You're trying to find the button using elem not row.
Perhaps using row.findElement(By.xpath("//button[@class='Click']")) will fix your issue.
答案2
得分: 0
要访问父元素的直接子元素,您需要提供.
Webelement button = row.findElement(By.xpath(".//button[@class='Click']"))
或者您可以使用这个方法:
Webelement button = driver.findElement(By.xpath("//div[//div[contains(text(), 'John Smith')]]/button[@class='Click']"))
两种方法都应该有效。
英文:
To access immediate child element of parent element you need to provide .
Webelement button = row.findElement(By.xpath(".//button[@class='Click']"))
Or you can use this
Webelement button = driver.findElement(By.xpath("//div[//div[contains(text(), 'John Smith']]/button[@class='Click']"))
Both should work.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论