`

WebElement.getText()为空解决方法

阅读更多

当使用getText()获取一个普通的链接文本时:

<a href="http://www.baidu.com">baidu</a>

如果得到的文本只为空,而非我们期望的baidu.那么尝试使用WebElement.isDisplayed()时候,将会得到false的结果.再尝试使用getAttribute("href"),发现能够争取获取href的值.由此可以说明:

  1. WebDriver判定isDisplayed为false的元素,那么getText()将为空
  2. isDisplayed为false的元素,依然可以通过getAttribute()方法获取元素的属性.

所以,当getText()为空的时候,可以通过两种方法获取链接的文本

  1. 修改页面当前元素,或者当前元素父元素的CSS,使元素的isDisplayed()值为true.
  2. 使用getAttribute("innerHTML")获取文本值

由此我们也可以知道,isDisplayed为false,是指那个元素是存在的,只是webdriver认为不见.回到关键问题,webdriver是如何判断一个元素是否displayed,请参照:

http://www.w3.org/TR/webdriver/#determining-visibility

 

事实上,我所要测试的link,在运行的时候并非隐藏而是可见的,运行中获取的CSS display值为block而非none.

大概是因为我的目标链接是在一个弹出下拉菜单中的原因

 

本文出自"lijingshou"博客,转载请务必保留此出处http://lijingshou.iteye.com/blog/2082704

分享到:
评论
1 楼 javapub 2017-09-04  
good point!

相关推荐

    week7:Selenium WebDriver,WebElement类的属性和方法

    第七周 高级Web驱动程序和Web元素类的属性和属性 ...使用WebDriver和WebElement类的各种方法和属性来实现与应用程序交互的测试 使用Select类自动执行下拉列表和列表 自动执行JavaScript警报和浏览器导航 HTML表单的

    README.txt

    import org.openqa.selenium.WebElement; import org.openqa.selenium.htmlunit.HtmlUnitDriver; import static junit.framework.Assert.assertNotNull; public class WebDriverTest { private ...

    SikuliRemoteWebdriver:sikuli驱动远程驱动版本

    你可以做一些像 WebElement we = sikuliremotedriverobject.findImage(url of image) we.getText(); 滚动整个页面以查找元素,而不仅仅是在视口中显示的区域。 示例程序: SikuliRemoteWebDriver driver = new ...

    JavaScript中计算网页中某个元素的位置

    由于项目的需要,测试中需要对网页元素进行截图,以确保它看上去没有问题。之前我写过一篇文章介绍过一种方法,先使用 WebDriver 进行全屏截图,然后...而 WebDriver 中的 WebElement.getLocation()方法只能返回这个 We

    selenium右击另存为图片

    解决怎么自动登陆网站,怎么进行右击另存为图片,实现了最大的便捷

    基于Python的selenium操作:判断元素是否存在+判断元素是否可以点击

    通过自己上网查找资料,在Python中成功 安装了selenium,同时在PyCharm里面也成功安装了selenium,并将下列三个小问对应的解决方法封装在一个类里。webelement_handler.py webelement_handler.py """使用selenium ...

    selenium.rar

    selenium python webdriver.Firefox webdriver.Ie webdriver.Chrome WebElement文档

    Android代码-matchers-java

    webdriver-matchers - Matchers library to work with WebDriver WebElement interface. Collections Matchers collection-matchers - Matchers library to work with collections. Common Matchers common-...

    QTP的隐藏button测试

    Browser(gBrowserTitle).page(gPageTitle).webelement("name:=HiddenPostButton","class:=hidden","innerText:=" & element.innertext,"html tag:=TD").click TempFlag =1 Exit for End If Next ...

    Selenium (WEB自动化工具/爬虫)

    本文详细介绍了Selenium Python版本的自动化测试方法和实践,通过本文学习你将掌握如下内容:Selenium 测试环境搭建单元测试、生成测试报告、定位元素、WebDriver属性和方法、WebElement属性和方法、操作form表单、...

    move to.py

    自己写的代码,可以参考下哦!来自于naoqi相关网站的学习,希望能够帮助到大家

    Javascript判断是否有ActiveX控件的两种方法

    Javascript判断是否有ActiveX控件的两种方法,在网页中使用ActiveX控件的添加方法。

    grafaces:Arquillian 石墨烯扩展

    多根您可以使用javax.annotation.PostConstruct注释无参数方法,然后该方法将在第一次访问 WebElement 的代理时执行。 这可以用来做一些初始化,比如在单选按钮组场景中检测属于同一组的所有单选按

    Last Java Collection Library:Java库与通常的Collections一起使用标量数组-开源

    该库还为仅具有Iterable接口的对象提供了诸如contains和indexOf之类的有用方法。 是的。 之所以有些愚蠢,是因为所有实现之间的区别仅在于类型之间的差异,并且此代码具有100%的隐式耦合和重复。 但是Java并没有别...

    ----AS3学习笔记

    ..................action script3,flex

    Java自动化测试系列[v1.0.0][智能等待]

    package util;...import org.openqa.selenium.WebElement; import org.openqa.selenium.support.ui.ExpectedCondition; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.

    harmonyos2-ie11-scroll-into-view:针对IE11在scrollIntoView()中的奇怪行为的Polyfill

    有多种解决方法,例如使用 jQuery,但这在运行 Selenium 测试时可能会出现问题,如下所示: WebElement element = driver . findElement( By . id( " my-id " )); Actions actions = new Actions (driver); actions ...

    web项目需要的jar包 (1).rar

    学习使用

    Java自动化测试系列[v1.0.0][模拟鼠标]

    import org.openqa.selenium.WebElement; import org.openqa.selenium.interactions.Actions; import org.openqa.selenium.WebDriver; public class MouseUtil { /** * 模拟鼠标左键单击 * @param driver * @...

    seleniumcapsules:Selenium胶囊-封装Selenium WebDriver

    Selenium胶囊 Selenium胶囊-封装Selenium 具有Java 8的功能编程功能的Selenium WebDriver API的包装。 你喜欢这个测试吗? //This is an ugly test not ... WebElement q = driver.findElement(By.name("q")); q

Global site tag (gtag.js) - Google Analytics