Reputation: 1237
Hey All I gave a question regarding xpath locator in selenium. I have a test that if I use the next code:
locator = By.xpath("//div[@class='ant-notification-notice ant-notification-notice-closable ng-trigger ng-trigger-notificationMotion']");`
everything is working since I user "class="
However if I change it and use contains :
locator = By.xpath("//div[contains(@class, 'ant-notification-notice ant-notification-notice-closable ng-trigger ng-trigger-notificationMotion')]");
I get
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')]"}
I do not understand what is the difference, and why If I use contains it not find.
Upvotes: 0
Views: 1611
Reputation: 193208
The xpath as:
//div[@class='ant-notification-notice ant-notification-notice-closable ng-trigger ng-trigger-notificationMotion']
is checking @class
attribute values lexically for the string:
ant-notification-notice ant-notification-notice-closable ng-trigger ng-trigger-notificationMotion
Where as xpath as:
//div[contains(@class, 'ant-notification-notice ant-notification-notice-closable ng-trigger ng-trigger-notificationMotion')]
is checking the substring ant-notification-notice ant-notification-notice-closable ng-trigger ng-trigger-notificationMotion
within the @class
attribute value.
I feel it would be a better idea to restrict the @class
attributes as follows:
//div[contains(@class, 'ant-notification-notice') and contains(@class, 'ant-notification-notice-closable')][contains(@class, 'ng-trigger') and contains(@class, 'ng-trigger-notificationMotion')]
You can find a relevant discussion in Why does google-chrome-devtools identifies less number of elements through XPath then number of elements identified through CssSelector
Upvotes: 0