首页 > 解决方案 > 无法在 http://automationpractice.com 上找到元素

问题描述

我使用下面的 xpath 在http://automationpractice.com上找到一个元素

driver.findElement(By.xpath("//a[@title = 'Choose from t-shirts, tops, blouses, short sleeves, long sleeves, tank tops, 3/4 sleeves and more. Find the cut that suits you the best!']")).click();

我不确定问题是什么。

如果我尝试使用 Linktext 定位它,它会起作用,但我想知道为什么上面的 xpath 不起作用。

标签: javaseleniumwebdriver

解决方案


因为属性值:title 包含新行,但您在一行中给出了所有字符串。

只需简单地将值复制到记事本中,即可得到以下字符串:

You will find here all woman fashion collections.  
 This category includes all the basics of your wardrobe and much more: 
 shoes, accessories, printed t-shirts, feminine dresses, women's jeans!

您可以使用字符串函数normalize-space()来删除新行和多余的空格。</p>

//a[normalize-space(@title)='Choose from t-shirts, tops, blouses, short sleeves, long sleeves, tank tops, 3/4 sleeves and more. Find the cut that suits you the best!']

推荐阅读