Compound class names are not supported. Consider searching for one class name and filtering the results(不支持复合类名.考虑搜索一个类名并过滤结果)
问题描述
我正在使用 driver.findelement by.classname 方法读取 Firefox 浏览器上的元素,但我得到不支持复合类名.考虑搜索一个类名并过滤结果."异常
I am using driver.findelement by.classname method to read an element on firefox browser but i am getting "Compound class names are not supported. Consider searching for one class name and filtering the results." exception
这是我的代码
driver.FindElement(By.ClassName("bighead crb")).Text.Trim().ToString()
//and here is how the html of browser looks like
<form action="#" id="aspnetForm" onsubmit="return false;">
<section id="lx-home" style="margin-bottom:50px;">
<div class="bigbanner">
<div class="splash mc">
<div class="bighead crb">LEAD DELIVERY MADE EASY</div>
</div>
</div>
</section>
</form>
推荐答案
不,就您的问题而言,您自己的答案并不是最好的.
No, your own answer isn't the best one in terms of your question.
想象一下你有这样的 HTML:
Imagine you have HTML like this:
<div class="bighead ght">LEAD DELIVERY MADE HARD</div>
<div class="bighead crb">LEAD DELIVERY MADE EASY</div>
driver.FindElement(By.ClassName("bighead"))
将找到两者并返回第一个 div
,而不是您想要的.您真正想要的是 driver.FindElement(By.ClassName("bighead crb"))
之类的东西,但是就像您在问题中所说的那样,这行不通,因为您需要另一种查找元素的方法按复合类名.
driver.FindElement(By.ClassName("bighead"))
will find both and return you the first div
, instead of the one your want. What you really want is something like driver.FindElement(By.ClassName("bighead crb"))
, but like you said in your question, this won't work as you need another way to find elements by compound class names.
这就是为什么大多数人使用更强大的 By.CssSelector
或 By.XPath
.那么你有:
This why most people use more powerful By.CssSelector
or By.XPath
. Then you have:
CssSelector(最好的):
CssSelector (the best):
driver.FindElement(By.CssSelector(".bighead.crb")); // flexible, match "bighead small crb", "bighead crb", "crb bighead", etc.
driver.FindElement(By.CssSelector("[class*='bighead crb']")); // order matters, match class contains "bighead crb"
driver.FindElement(By.CssSelector("[class='bighead crb']")); // match "bighead crb" strictly
XPath(更好):
driver.FindElement(By.XPath(".//*[contains(@class, 'bighead') and contains(@class, 'crb')]")); // flexible, match "bighead small crb", "bighead crb", "crb bighead", etc.
driver.FindElement(By.XPath(".//*[contains(@class, 'bighead crb')]")); // order matters, match class contains string "bighead crb" only
driver.FindElement(By.XPath(".//*[@class='bighead crb']")); // match class with string "bighead crb" strictly
这篇关于不支持复合类名.考虑搜索一个类名并过滤结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:不支持复合类名.考虑搜索一个类名并过滤结果
基础教程推荐
- rabbitmq 的 REST API 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01