XPath-selenium中contains和equals之间的差异

XPath-selenium中contains和equals之间的差异,selenium,selenium-webdriver,xpath,webdriver,xpath-1.0,Selenium,Selenium Webdriver,Xpath,Webdriver,Xpath 1.0,嘿,我问了一个关于selenium中xpath定位器的问题。 我有一个测试,如果我使用下一个代码: locator = By.xpath("//div[@class='ant-notification-notice ant-notification-notice-closable ng-trigger ng-trigger-notificationMotion']");` 自I用户“class=”起,一切正常 但是,如果我更改它并使用它,则包含: locator = By.xpath("//d

嘿,我问了一个关于selenium中xpath定位器的问题。 我有一个测试,如果我使用下一个代码:

locator = By.xpath("//div[@class='ant-notification-notice ant-notification-notice-closable ng-trigger ng-trigger-notificationMotion']");`
自I用户“class=”起,一切正常

但是,如果我更改它并使用它,则包含:

locator = By.xpath("//div[contains(@class, 'ant-notification-notice ant-notification-notice-closable ng-trigger ng-trigger-notificationMotion')]");
我明白了

ERROR: no such element: Unable to locate element: {"method":"xpath","selector":"//div[contains(@class, 'ant-notification-notice ant-notification-notice-closable ng-trigger ng-trigger-notificationMotion')]"}
我不明白有什么区别,为什么我使用contains却找不到它。

xpath为:

正在按词汇检查字符串的
@class
属性值:

ant-notification-notice ant-notification-notice-closable ng-trigger ng-trigger-notificationMotion
其中xpath为:

正在检查
@class
属性值内的子字符串
ant notification notification ant notification closable ng trigger ng trigger notificationMotion

我觉得最好限制
@class
属性,如下所示:

//div[contains(@class, 'ant-notification-notice') and contains(@class, 'ant-notification-notice-closable')][contains(@class, 'ng-trigger') and contains(@class, 'ng-trigger-notificationMotion')]
您可以在中找到相关的讨论


它应该可以工作,我猜这是时间问题,而不是xpath问题。请尝试等待几秒钟进行检查。第二种情况下的此xpath应已捕获第一种xpath。是否检查了有多少与包含xpath的匹配元素。如果只有一个,它应该按预期工作。否则,如果有多个匹配元素,请更改xpath或pass索引以标识其中的预期元素。是的,只有一个元素,我认为这是时间问题,因为它有时工作,有时不工作。这无法回答问题和/或不正确。如果您正在查找@class='x'并且它找到了它,那么使用contains(@class='x')也可以。将每个类分解为单独的字符串也不能解释或解决问题。这可能是时间问题。
//div[contains(@class, 'ant-notification-notice ant-notification-notice-closable ng-trigger ng-trigger-notificationMotion')]
//div[contains(@class, 'ant-notification-notice') and contains(@class, 'ant-notification-notice-closable')][contains(@class, 'ng-trigger') and contains(@class, 'ng-trigger-notificationMotion')]