J.dockster
J.dockster

Reputation: 43

Creating own XPath: C# + Selenium

I've written a test which adds an item to the bag. My test is working as I expect but I'm having trouble creating my own XPath. I have managed to inspect the "Add to basket" table and copied the XPath provided but I'm unsure of how to make my own from what is being given.

Link for the item I want to add to the bag:

http://www.asos.com/nike/nike-air-force-1-07-trainers-in-white-315122-111/prd/4756254?clr=white&SearchQuery=nike%20trainers&gridcolumn=1&gridrow=1&gridsize=4&pge=1&pgesize=72&totalstyles=504

Information provided when inspect the "Add to bag" tab:

<span data-bind="text: buttonText">Add to bag</span>

Code:

webDriver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(5);
webDriver.FindElement(By.XPath("//*[@id='product-add']/div/a/span[2]")).Click();

Upvotes: 0

Views: 90

Answers (2)

Chloe Corrigan
Chloe Corrigan

Reputation: 371

Are you requesting something like this?

"//a[@aria-label='Add to bag']"

(tested this and it worked for me!)

Above is essentially checking all anchor tags for the one with aria-label 'Add to bag'.

This would assure you're clicking on the link specific to 'Add to bag' and is pretty obvious in the xpath when reading the code.

Here's some more info on xpath building.

As a side note, I would also suggest explicit waits for your xpaths.

Upvotes: 0

Ratmir Asanov
Ratmir Asanov

Reputation: 6459

Try the following XPath:

//*[@data-bind='text: buttonText']

Hope it helps you!

Upvotes: 1

Related Questions