Selenium学习笔记(三)——访问连接和表格&鼠键事件

来源:互联网 发布:mysql恢复删除的数据 编辑:程序博客网 时间:2024/05/21 03:57

访问连接

匹配方式

访问连接的方式有两种:
1. 准确匹配 By.linkText
2. 部分匹配 By.partialLinkText

两者均是大小写敏感,且只依据连接显示的名称,不受任何格式标签或块标签的影响,如:

<a><em>Hello</em> World</a><a><div>Hello World</div></a>

检查连接

检查页面上的连接是否有效是网页测试常见的任务。我们可以通过By.tagName(“a”)来获取,然后通过逐个点击检查。部分代码:

driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);driver.get(targetUrl);List<WebElement> links = driver.findElements(By.tagName("a"));//check linksint linkSize = links.size();for(int i=0; i<linkSize; i++){    String linkText = links.get(i).getText();    links.get(i).click();    //if the linked page's title equals to the predefined under construction title    if(driver.getTitle().equals(underConstructionTitle)){        System.out.println("\"" + linkText + "\""                            + " is under construction.");    }else{        System.out.println("\"" + linkText + "\""                            + " is working.");    }    driver.navigate().back();}dirver.quit();

图片连接

因为图片连接不具有文字,所以我们不能使用By.linkText()和By.partialLinkText()。在这种情况下,我们应该使用By.cssSelector()或By.xpath()。
页面代码:

<a title="Go to Facebook Home" href="xxx">    <img src="Facebook logo.jpg"/></a>

访问代码:

driver.findElement(By.cssSelector("a img[src='Facebook logo.jpg']"));

读取表格

因为网页设计的时候很少给特定的表格单元添加id或name属性,所以我们不能用常规的方法访问表格。在这种情况下,最可靠的方式是通过By.xpath()方法访问他们。
若表格如下:

<html>    <body>        <table border="1">            <tbody>                <tr>                    <td>1</td>                    <td>2</td>                </tr>                <tr>                    <td>3</td>                    <td>4</td>                </tr>            </tbody>        </table>    </body></html>

则可以通过以下代码获取4单元的信息:

new WebDriver().findElement(By.xpath("//table/tbody/tr[2]/td[2]"));

鼠标键盘操作

在处理特殊的键盘和鼠标事件时,我们可以使用AdvancedUserInteractions API。它包含了执行这些事件所需的Action和Actions类。下面是Actions类提供的一些最常用的鼠键事件:

Method Description clickAndHold() Clicks (without releasing) at the current mouse location. contextClick() Performs a context-click at the current mouse location. doubleClick() Performs a double-click at the current mouse location. dragAndDrop(source, target) Performs click-and-hold at the location of the source element, moves to the location of the target element, then releases the mouse.
Parameters:
source- element to emulate button down at.
target- element to move to and release the mouse at. dragAndDropBy(source, x-offset, y-offset) Performs click-and-hold at the location of the source element, moves by a given offset, then releases the mouse.
Parameters:
source- element to emulate button down at.
xOffset- horizontal move offset.
yOffset- vertical move offset. keyDown(modifier_key) Performs a modifier key press. Does not release the modifier key - subsequent interactions may assume it’s kept pressed.
Parameters:
modifier_key - any of the modifier keys (Keys.ALT, Keys.SHIFT, or Keys.CONTROL) keyUp(modifier _key) Performs a key release.
Parameters:
modifier_key - any of the modifier keys (Keys.ALT, Keys.SHIFT, or Keys.CONTROL) moveByOffset(x-offset, y-offset) Moves the mouse from its current position (or 0,0) by the given offset.
Parameters:
x-offset- horizontal offset. A negative value means moving the mouse left.
y-offset- vertical offset. A negative value means moving the mouse up. moveToElement(toElement) Moves the mouse to the middle of the element.
Parameters:
toElement- element to move to. release() Releases the depressed left mouse button at the current mouse location sendKeys(onElement, charsequence) Sends a series of keystrokes onto the element.
Parameters:
onElement - element that will receive the keystrokes, usually a text field
charsequence - any string value representing the sequence of keystrokes to be sent