1

I am trying to create a proper XPATH syntax in C# to click the delete button from the Amazon cart screen. There could be one item or multiple items in the cart so I'm looking to loop through the delete buttons. I could not get it to find the Delete button by just doing a contains the word Delete. I finally said okay let me just find all of the buttons on the screen. I will loop through them and use the text from each link to figure out which one I need to click. The only problem with that was that it indeed found 20 different items(("//input[@type='submit']")) on the page, but the Text field was empty for all. I'm new to this XPATH syntax and it's really causing me problems. Any help would be appreciated.

Here are some of the many things I tried along with a screen shot of pertinent info:

var links = driver.FindElements(By.XPath("//table[contains(@name, 'ReportViewer_fixedTable')]"));
var links = driver.FindElements(By.XPath("//css=a[name^='submit.delete']"));
var links = driver.FindElement(By.CssSelector("table[id*='ReportViewer_fixedTable']"));
var links = driver.FindElements(By.XPath("//button[@type='submit'][text()='Delete']")); WORKED DIDNT FIND ANYTHING
var links = driver.FindElements(By.XPath("//input[@type='submit'][contains(text(),'Delete')]"));  //WOREKED DIDNT FIND ANYTHING
var links = driver.FindElements(By.XPath("//input[@type='submit']"));
var links = driver.FindElements(By.XPath("//div[@class='a-row sc-action-links']/span[@class='a-declarative']"));

Inspection from Chrome: enter image description here

And What my output looks like when I loop through just finding //input[@type='submit']. You can see Text is empty. It's that way for all of them.

enter image description here

2
  • Do you want to click any particular delete button or get all button value?? Commented Sep 14, 2016 at 22:19
  • //input[starts-with(@name,"submit.delete")] Commented Sep 14, 2016 at 22:38

1 Answer 1

3

In xpath text() matches text node children of the context node while input has no child elements with desire text Delete.

Actually Delete text present in value attribute of input element, so you should try to match with Delete text with value attribute as @value = 'Delete'.

So to find all delete buttons using Xpath try as below :-

var deleteButtons = driver.FindElements(By.XPath("//input[@type='submit'][@value='Delete']")); 

Or using CssSelector as below :-

var deleteButtons = driver.FindElements(By.CssSelector("input[type='submit'][value='Delete']")); 

UPDATE: For anyone that may need this in the future. This is the finished product:

var deleteButtons = driver.FindElements(By.CssSelector("input[type='submit'][value='Delete']"));

        foreach (var link in deleteButtons)
        {
            link.Click();
        }
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you good sir. This one worked like a charm. var deleteButtons = driver.FindElements(By.CssSelector("input[type='submit'][value='Delete']"));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.