我正在尝试自动化一个手动脚本(使用java中的selenium )来检查网页上某个字段(label:代表强制字段)的粗体外观。什么是可能的selenium java函数来验证某些元素的粗体外观(在类中没有关于外观的信息)
发布于 2012-04-11 22:37:17
您可以使用style()方法检查font-weight (假设您实际使用的是Selenium-Webdriver)。
假设你有这样的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>您可以执行以下操作来检查字段标签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 发布于 2012-04-13 22:55:14
对于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)
使用Selenium RC,请参阅this technique,只需使用font-weight (或fontWeight,视用法而定)。
发布于 2013-01-16 23:26:36
我真的很喜欢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
很抱歉,这是一个单独的答案,而不是对那个答案的评论,但我显然有太低的因果关系阈值,无法在这里发表评论
https://stackoverflow.com/questions/10100438
复制相似问题