C# Selenium 循环通过跨度类 XPATH 中的输入按钮 [英] C# Selenium loop through Input Buttons in a Span Class XPATH

查看:29
本文介绍了C# Selenium 循环通过跨度类 XPATH 中的输入按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 C# 中创建正确的 XPATH 语法,以从亚马逊购物车屏幕单击删除按钮.购物车中可能有一件或多件物品,因此我希望遍历删除按钮.我无法通过仅执行包含单词 Delete 的操作来找到删除"按钮.我终于说好吧,让我找到屏幕上的所有按钮.我将遍历它们并使用每个链接中的文本来确定我需要单击哪个链接.唯一的问题是它确实在页面上找到了 20 个不同的项目(("//input[@type='submit']")),但所有的 Text 字段都是空的.我是这个 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']"));

来自 Chrome 的检查:

Inspection from Chrome:

当我循环查找//input[@type='submit'] 时,我的输出是什么样的.您可以看到 Text 为空.他们所有人都是这样.

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.

推荐答案

xpath text() 匹配上下文节点的文本节点子节点,而 input 没有想要文本 Delete 的子元素.

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

实际上 Delete 文本存在于 input 元素的 value 属性中,因此您应该尝试与 value 属性为 Delete 文本匹配>@value = '删除'.

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'.

所以要使用 Xpath 查找所有删除按钮,请尝试如下:-

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

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

或者使用 CssSelector 如下:-

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

这篇关于C# Selenium 循环通过跨度类 XPATH 中的输入按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆