Java + Selenium(七) 常见的元素处理-upfile

常见的元素处理-upfile

多选框

  • sendkeys
package com.test.selenium;

import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class ActionSelenium {
	public WebDriver driver;
	
	public void InitDriver(){
		System.setProperty("webdriver.firefox.bin","C:\\Program Files\\Mozilla Firefox\\firefox.exe");
		driver = new FirefoxDriver();
		driver.navigate().to("https://www.imooc.com/user/newlogin");
		driver.manage().window().maximize();
	}
	public void inputBox(){
		//driver.findElement(By.name("email")).sendKeys("895236987@qq.com");
		try{
			Thread.sleep(2000);
		}catch(InterruptedException e){
			e.printStackTrace();
		}
		//driver.findElement(By.name("email")).clear();
		//String s = driver.findElement(By.name("email")).getAttribute("placeholder");
		//System.out.println(s);
		driver.findElement(By.name("email")).sendKeys("895236987@qq.com");
		driver.findElement(By.name("password")).sendKeys("sder");
		//driver.findElement(By.className("moco-btn-red")).click();
		try{
			Thread.sleep(4000);
		}catch(InterruptedException e){
			e.printStackTrace();
		}
	}
	
	/**
	 * 单选框
	 * 
	 * */
	public void radioBox(){
		driver.get("https://www.imooc.com/user/setprofile");
		driver.findElement(By.className("pull-right")).click();
		//driver.findElement(By.xpath(".//*[@id='profile']/div[4]/div/label[1]")).click();
		List<WebElement> elements = driver.findElements(By.xpath(".//*[@id='profile']/div[4]/div/label//input"));
		System.out.println(elements.size());
		try{
			Thread.sleep(2000);
		}catch(InterruptedException e){
			e.printStackTrace();
		}
		for(WebElement radio:elements){
			boolean flag = radio.isSelected();
			if(flag==false){
				radio.click();
				break;
			}else{
				System.out.println("选中了");
			}
		}
	}
	/**
	 * 复选框
	 * */
	public void checkBox(){
		WebElement check = driver.findElement(By.id("auto-signin"));
		System.out.println("是否选中了呢?" + check.isSelected());
		System.out.println("是否有效?" + check.isEnabled());
		//check.clear();  //不需要使用
		try{
			Thread.sleep(3000);
		}catch(InterruptedException e){
			e.printStackTrace();
		}
		check.click();
	}
	
	/**
	 * 按钮
	 * */
	public void button(){
		WebElement login = driver.findElement(By.className("moco-btn"));
		System.out.println(login.isEnabled());
		System.out.println(login.getAttribute("value"));
		login.click();
	}
	
	/**
	 * 表单
	 * */
	public void webForm(){
		driver.get("XXXXX");
		driver.findElement(By.id("signup-form")).submit();
	}	
	
	/**
	 * 上传文件
	 * */
	public void upHeader(){
		
		try {
			Thread.sleep(2000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		driver.get("https://www.imooc.com/user/setbindsns");
		driver.findElement(By.className("js-avator-link")).click();
		driver.findElement(By.id("upload")).sendKeys("C:\\Users\\Public\\Pictures\\Sample Pictures\\logd.png");
		
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		ActionSelenium as = new ActionSelenium();
		as.InitDriver();
		as.inputBox();
		//as.checkBox();
		as.button();
		as.upHeader();
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值