简体   繁体   中英

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: 在此输入图像描述

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.

在此输入图像描述

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();
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM