zoukankan      html  css  js  c++  java
  • 【Web自动化测试——代码篇四】常用方法——常见元素操作

    浏览器这个大框架我们都能控制,区区页面小元素又能奈我们何!!!之前的【Web自动化测试——代码篇二】条条大路找元素 已经讲述了许多获取元素的方法,在此基础上我们来认识几个元素最常见的相关操作ԅ(¯﹃¯ԅ)

      Java Python Ruby
    输入内容 sendKeys(*value) send_keys(*value) send_keys(*value)
    单击元素 click() click() click
    清除文本 clear() clear() clear
    提交表单 submit() submit() submit
    获取元素的文本 getText() text text
    获取元素的指定属性值 getAttribute(String attributeName) get_attribute(String attributeName) attribute(String attributeName)

    **代码时间 **

    (。﹏。*)其实吧,“获取元素的文本”这个方法我是想获取 百度输入框 的文本的,但是不知道为啥虽然代码不报错却一直获取不到它的文本(难道是因为它是input文本框的问题?╮(╯▽╰)╭)。所以我就想既然我们有“获取元素的指定属性值”这个方法,那我们就直接获取它的value属性值<( ̄︶ ̄)↗[GO!]

    Java

     1 package JavaTest;
     2 
     3 import org.openqa.selenium.By;
     4 import org.openqa.selenium.WebDriver;
     5 import org.openqa.selenium.firefox.FirefoxDriver;
     6 
     7 public class Test {
     8     public static void main(String[] arg) throws InterruptedException
     9     {
    10         WebDriver driver = new FirefoxDriver();
    11         driver.get("http://www.baidu.com/");
    12 
    13         driver.findElement(By.id("kw")).sendKeys("Java");  //对百度输入框赋值
    14         driver.findElement(By.id("kw")).submit();  //对百度输入框提交
    15                 System.out.println(driver.findElement(By.id("kw")).getAttribute("value")); //获取百度输入框值
    16         Thread.sleep(2000);
    17         driver.findElement(By.id("kw")).clear();  //清除百度输入框
    18         Thread.sleep(2000);
    19         driver.findElement(By.id("su")).click();  //点击百度搜索按钮
    20         Thread.sleep(2000);
    21         System.out.println(driver.findElement(By.id("cp")).getText());  //显示百度备案信息文本
    22 
    23         driver.close();  //结束
    24     }
    25 }

     

    Python

     1 from time import *
     2 from selenium import webdriver
     3 from selenium.webdriver.common.by import By
     4 
     5 # 启动Firefox浏览器
     6 driver = webdriver.Firefox()
     7 driver.get('http://www.baidu.com')
     8 
     9 driver.find_element(By.XPATH,"//*[@class='s_ipt']").send_keys('Python')  # 对百度输入框赋值
    10 driver.find_element_by_xpath("//input[@type='submit']").submit()  # 对百度输入框提交
    11 print(driver.find_element(By.ID,'kw').get_attribute('value')) # 获取百度输入框值
    12 sleep(2) # 休眠2秒
    13 driver.find_element(By.ID,'kw').clear()  # 清除百度输入框
    14 sleep(2)
    15 driver.find_element_by_id('su').click()  # 点击百度搜索按钮
    16 sleep(2)
    17 print(driver.find_element(By.ID,'cp').text)  # 显示百度备案信息文本
    18 
    19 driver.close() # 结束

     

    Ruby

     1 class Baidu
     2   require 'rubygems'
     3   require 'selenium-webdriver'
     4 
     5   #打开firefox并输入网址
     6   driver = Selenium::WebDriver.for :firefox
     7   driver.navigate.to "http://www.baidu.com"
     8 
     9   driver.find_element(:name, 'wd').send_keys('Ruby')  # 对百度输入框赋值
    10   driver.find_element(:id, 'kw').submit # 百度输入框提交
    11   puts driver.find_element(:id, 'kw').attribute('value') # 获取百度输入框值
    12   sleep(2) # 休眠2秒
    13   driver.find_element(:xpath, "//*[@class='s_ipt']").clear # 清除百度输入框
    14   sleep(2)
    15   driver.find_element(:id, 'su').click # 点击百度搜索按钮
    16   sleep(2)
    17   puts driver.find_element(:id, 'cp').text
    18 
    19   driver.close #退出程序
    20 end
  • 相关阅读:
    Hadoop集群(三) Hbase搭建
    Hadoop集群(二) HDFS搭建
    Hadoop集群(一) Zookeeper搭建
    Redis Cluster 添加/删除 完整折腾步骤
    Redis Cluster在线迁移
    Hadoop分布式HA的安装部署
    Describe the difference between repeater, bridge and router.
    what is the “handover” and "soft handover" in mobile communication system?
    The main roles of LTE eNodeB.
    The architecture of LTE network.
  • 原文地址:https://www.cnblogs.com/CSgarcia/p/9413791.html
Copyright © 2011-2022 走看看