问题描述
我正在使用 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
这篇关于不支持复合类名.考虑搜索一个类名并过滤结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!


大气响应式网络建站服务公司织梦模板
高端大气html5设计公司网站源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账网站织梦模板(带手机端)
成人高考自考在职研究生教育机构网站源码(带手机端)
高端HTML5响应式企业集团通用类网站织梦模板(自适应手机端)