在http://automationpractice.com上找不到元素

时间:2018-08-13 00:36:50

标签: java selenium webdriver

我使用下面的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无法起作用。

1 个答案:

答案 0 :(得分:0)

因为attribute: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()删除新行和多余的空间。

//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!']