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