Roro Roro - 1 year ago 95
C# Question

C# Selenium loop through Input Buttons in a Span Class XPATH

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

Answer Source

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']"));