java 如何验证硒中某个字段的粗体外观
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10100438/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to verify bold appearance of a certain field in selenium
提问by Virendra Joshi
I am trying to automate a manual script (using selenium in java) to check the bold appearance of a certain field(label:which stands for mandatory field) on a web page . what can be the possible selenium java functions to verify the bold appearance of certain element(In class there is no information about the appearance)
我正在尝试自动化手动脚本(在 Java 中使用 selenium)来检查网页上某个字段(标签:代表必填字段)的粗体外观。什么是可能的 selenium java 函数来验证某些元素的粗体外观(在课堂上没有关于外观的信息)
采纳答案by Justin Ko
You can check the font-weightusing the style()
method (assuming you are actually using Selenium-Webdriver).
您可以使用该方法检查字体粗细style()
(假设您实际上使用的是 Selenium-Webdriver)。
So say you have HTML like:
所以说你有这样的 HTML:
<body>
<div id='1' style='font-weight:normal'>
<div id='2' style='font-weight:bold'>Field Label</div>
<div id='3'>Field</div>
</div>
</body>
You can do the following to check the font-weight of the field label div (the following is in Ruby, though similar should be possible in the other languages).
您可以执行以下操作来检查字段标签 div 的字体粗细(以下是在 Ruby 中,但在其他语言中应该类似)。
el = driver.find_element(:id, "2")
if el.style('font-weight') >= 700
puts 'text is bold'
else
puts 'text is not bold'
end
回答by Petr Jane?ek
With WebDriver (in Java), you can use getCssValue().
使用 WebDriver(在 Java 中),您可以使用getCssValue()。
import static org.junit.Assert.assertTrue;
(...)
// assuming elem is a healthy WebElement instance, your found element
String fontWeight = elem.getCssValue("font-weight");
assertTrue(fontWeight.equals("bold") || fontWeight.equals("700"));
(since 700
is the same as bold
)
With Selenium RC, see this technique, just use font-weight
(or fontWeight
depending on the usage).
使用 Selenium RC,请参阅此技术,只需使用font-weight
(或fontWeight
取决于使用情况)。
回答by Rob Dennis
I really do like Justin Ko's suggestion to use the style("font-weight")
method, however in the python bindings, the equivalent appears to be value_of_css_property("font-weight")
我真的很喜欢 Justin Ko 的使用该style("font-weight")
方法的建议,但是在 python 绑定中,等效的似乎是value_of_css_property("font-weight")
>>> element = self.wd.find_element_by_id("some-id")
>>> element.value_of_css_property('font-weight')
u'700'
>>> element.style('font-weight')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'WebElement' object has no attribute 'style'
http://code.google.com/p/selenium/source/browse/py/selenium/webdriver/remote/webelement.py#132
http://code.google.com/p/selenium/source/browse/py/selenium/webdriver/remote/webelement.py#132
apologies that this is a separate answer versus a comment for that answer, but I apparently have too low of a karma threshold to comment there
很抱歉,这是一个单独的答案而不是对该答案的评论,但我显然没有在那里发表评论的业力阈值太低
回答by Bob Evans
I see that the element you mention has a compound class, why not try to locate all the elements with the label_required class, python example
我看到你提到的元素有一个复合类,为什么不尝试使用 label_required 类来定位所有元素,python 示例
els = driver.find_elements_by_css_selector(div[class*=label_required])
self.assertTrue(len(els) == [known value of required fields on page])
java(Note: I don't write java code, so syntax may be wrong):
java(注意:我不会写java代码,所以语法可能有误):
import static org.junit.Assert.assertTrue;
WebDriver driver = new FirefoxDriver();
WebElement els=driver.findElements(By.cssSelector("div[class*=label_required]"));
assertTrue(els.length == [known value of required fields on page]);