seleniu 元素定位与操作

Posted yuyanhzao

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了seleniu 元素定位与操作相关的知识,希望对你有一定的参考价值。

一 元素定位

1.通过id定位:driver.findElement( By.id("Text1"))

2.通过name定位:driver.findElement( By.name("输入"))

3.通过xpath定位:driver.findElement( By.xpath("html/body/div[1]/p[1]/label/input"))

4.通过linkText定位链接:driver.findElement( By.linkText ("这是一个链接")),需要完整匹配文字

5.通过partialLinkText定位链接:driver.findElement( By. partialLinkText ("链接"))

6.定位多个元素:返回list,List elements,通过elements.get(index).click(),进行遍历操作,index从0开始

二 元素操作

1.文本框赋值:sendKeys(“你好!”)

2.清空文本框:clear()

3.获取文本框中的内容:getAttribute("value")

4.点击:.click()

5.下拉框:(1)选择,首先第一步,定义一个下拉框对象Select select =new Select(driver.findElement(By.xpath("//*[@id=‘select_k1‘]")))

                                      第二步,通过对象定位所选的下拉框内容,三种方法:通过可见文本去选择,select.selectByVisibleText("哈尔滨");通过html中的value值去选择,select.selectByValue("heb");通过index(索引从0开始)选择,select.selectByIndex(2);

                                      第三步,获取下拉框已选值:使用getFirstSelectedOption()获得所选的想,再通过getText()获得值,select.getFirstSelectOption().getText()

6.复选框

7.单选按钮:选中,click();判断单选框是否被选中,返回布尔值,isSelected()

8.页面弹框处理(alert):第一步,声明对话框变量,Alert alert = driver.switchTo().alert();

                                          第二步,获取弹框中的文字,alert.getText();

                                          第三步,点击弹框中的确定,alert.accept();

9.表单(form):

以上是关于seleniu 元素定位与操作的主要内容,如果未能解决你的问题,请参考以下文章

软件测试开发学习之旅----Selenium

CSS代码片段

CSS代码片段

Selenium Xpath元素无法定位 NoSuchElementException: Message: no such element: Unable to locate element(代码片段

如何使绝对定位的元素响应?

Webdriver API(元素定位与操作)