在Selenium WebDriver中区分b / w getText()和getAttribute()?
两者都用于获取标签之间的WebElement值? 请更新我的假设是否正确。 如果错了请详细说明。
foo
getAttribute(attr1)
你得到’a’
getAttribute(attr2)
你得到’b’
getAttribute(attr3)
你得到’c’
没有参数的getText()
你只能获得’foo’
getAttribute() – >它获取包含HTML标记中任何属性之一的文本。 假设有像这样的HTML标签
Hello
现在getAttribute()获取值为“Selenium”的属性的数据
返回:属性的当前值,如果未设置该值,则返回null。
driver.findElement(By.name("Name Locator")).getAttribute("value") // The field value is retrieved by the getAttribute("value") Selenium WebDriver predefined method and assigned to the String object.
getText() – >提供WebElement的innerText。 获取此元素的可见(即不被CSS隐藏)innerText,包括子元素,没有任何前导或尾随空格。
返回:此元素的innerText。
driver.findElement(By.name("Name Locator")).getText();
你好会出现
在上面的html标签中,我们有不同的属性,如src
, alt
, width
和height
。
如果要从html标记上方获取any属性值,则必须在getAttribute()
方法中传递属性值
句法:
getAttribute(attributeValue) getAttribute(src) you get https://stackoverflow.com/questions/32307702/difference-b-w-gettext-and-getattribute-in-selenium-webdriver/w3schools.jpg getAttribute(height) you get 142 getAttribute(width) you get 104
getText():获取此元素的可见(即不被CSS隐藏)innerText,包括子元素,没有任何前导或尾随空格。
getAttribute(String attrName):获取元素的给定属性的值。 将返回当前值,即使在页面加载后已经修改了该值。 更确切地说,此方法将返回给定属性的值,除非该属性不存在,在这种情况下,将返回具有相同名称的属性的值(例如,对于textarea元素的“value”属性)。 如果未设置任何值,则返回null。 “style”属性最好转换为带有尾部分号的文本表示。 以下被认为是“布尔”属性,并将返回“true”或null:async,autofocus,autoplay,checked,compact,complete,controls,declare,defaultchecked,defaultselected,defer,disabled,draggable,ended,formnovalidate ,隐藏,不确定,iscontenteditable,ismap,itemscope,循环,多个,静音,nohref,noresize,noshade,novalidate,nowrap,打开,暂停,发布,readonly,必需,反向,作用域,无缝,寻求,选择,拼写检查,truespeed ,willvalidate最后,按预期方式评估以下通常错误大写的属性/属性名称:“class”“readonly”
getText()
返回元素的可见文本。
getAttribute(String attrName)
返回作为参数传递的属性的值。