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
  • 相关阅读:
    BZOJ 2002: [Hnoi2010]Bounce 弹飞绵羊
    算法笔记-- 二进制集合枚举子集 && 求子集和 && 求父集和
    BZOJ 1084: [SCOI2005]最大子矩阵
    BZOJ 1968: [Ahoi2005]COMMON 约数研究
    2018 German Collegiate Programming Contest (GCPC 18)
    Codeforces 1100 F
    算法笔记--极大极小搜索及alpha-beta剪枝
    2017 Russian Code Cup (RCC 17), Elimination Round D
    All You Can Code 2008 (Romanian Contest) A
    2016 Russian Code Cup (RCC 16), Final Round B
  • 原文地址:https://www.cnblogs.com/CSgarcia/p/9413791.html
Copyright © 2011-2022 走看看