51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

查看: 1220|回复: 1
打印 上一主题 下一主题

查找条件对象

[复制链接]
  • TA的每日心情
    开心
    2022-2-14 16:26
  • 签到天数: 1 天

    连续签到: 1 天

    [LV.1]测试小兵

    跳转到指定楼层
    1#
    发表于 2022-2-14 16:32:20 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    16.5.1 Id(idToFind)
    按html 元素的id属性查找元素
    driver.get("file:///C:/exam/House.html");
    WebElement inputKey=driver.findElement(By.id("email"));
    inputKey.clear();
    inputKey.sendKeys("test@test.com");

    16.5.2  Name(nameToFind)
    按html 元素的name属性查找元素
      driver.get("file:///C:/exam/House.html");
              WebElement inputKey=driver.findElement(By.name("username"));
              inputKey.clear();
              inputKey.sendKeys("test");
    16.5.3  LinkText(linkTextToFind)
    按html 元素的按链接的文本进行查找查找元素
    driver.get("file:///C:/exam/index.html");
    WebElement clickLink=driver.findElement(By.linkText("继续提供房源信息"));
    Thread.sleep(3);
    clickLink.click();
    16.5.4  partialLinkText
    按html 元素的按链接的文本进行进行模糊查找元素
            driver.get("file:///C:/exam/index.html");
              WebElement clickLink=driver.findElement(By.partialLinkText("提供房源"));
              Thread.sleep(3);
              clickLink.click();
    16.5.5  className(classzNameToFind)
    按元素的class 属性进行查找
      driver.get("file:///C:/exam/House.html");       
              WebElement inputBox=driver.findElement(By.className("uname"));
              inputBox.clear();
              inputBox.sendKeys("test");
              

    16.5.6  TagName(TagNameToFind)
    按标记名称查找
              driver.get("file:///C:/exam/index.html");       
              WebElement linkText=driver.findElement(By.tagName("a"));
              linkText.click();

    实例: testlist 代码
    <!doctype html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <meta name="Generator" content="EditPlus®">
      <meta name="Author" content="">
      <meta name="Keywords" content="">
      <meta name="Description" content="">
      <title>Document</title>
    </head>
    <body>
      <table border=1 cellspacing="0">
      <tr>
            <td>姓名</td>
            <td>代码基础</td>
            <td>测试理论</td>
            <td>测试工具</td>
      </tr>
      <tr>
            <td>史宝宝</td>
            <td>90</td>
            <td>98</td>
            <td>88</td>
      </tr>
      <tr>
            <td>吴迪</td>
            <td>88</td>
            <td>90</td>
            <td>98</td>
      </tr>
      <tr>
            <td>徐亮亮</td>
            <td>77</td>
            <td>88</td>
            <td>99</td>
      </tr>
      <tr>
            <td>涛涛</td>
            <td>89</td>
            <td>79</td>
            <td>99</td>
      </tr>
      </table>
    </body>
    </html>

      driver.get("http://192.168.2.160/test/testlist.html");
          WebElement table=driver.findElement(By.xpath("//table"));
          List<WebElement> rows=table.findElements(By.tagName("tr"));
          for(WebElement row:rows){
                  List<WebElement> cols=row.findElements(By.tagName("td"));
                  for(WebElement col:cols){
                          System.out.print(col.getText()+" ");
                  }
                  System.out.println();
    16.5.7  XPath(xPathToFind)
      driver.get("file:///C:/exam/House.html");       
              WebElement inputBox=driver.findElement(By.xpath("//input[@name='username']"));
              inputBox.clear();
              inputBox.sendKeys("test");
    16.5.8  cssSelect
    driver.get("file:///C:/exam/House.html");       
              WebElement inputBox=driver.findElement(By.cssSelector("input.uname"));
              inputBox.clear();
              inputBox.sendKeys("test");
    分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
    收藏收藏
    回复

    使用道具 举报

  • TA的每日心情
    开心
    前天 13:32
  • 签到天数: 1195 天

    连续签到: 3 天

    [LV.10]测试总司令

    2#
    发表于 2022-5-26 13:44:12 | 只看该作者
    咱可以加点文字说明就更好了
    回复 支持 反对

    使用道具 举报

    本版积分规则

    关闭

    站长推荐上一条 /1 下一条

    小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

    GMT+8, 2024-4-27 05:42 , Processed in 0.067127 second(s), 22 queries .

    Powered by Discuz! X3.2

    © 2001-2024 Comsenz Inc.

    快速回复 返回顶部 返回列表