Java+Selenium3方法篇29-Actions-划取字段

        本文介绍Actions类下第二个功能,划取字段。我之前做过一个项目,需要在一堆log字符中随机划取一段文字,然后右键选择摘取功能。这篇,就介绍划取字段这个功能,直接看下面的脚本。

package lessons;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;

public class MyClass {

	public static void main(String[] args) throws InterruptedException {
		 
        System.setProperty("webdriver.gecko.driver", ".\\Tools\\geckodriver.exe");  
           
        WebDriver driver = new FirefoxDriver();
		
		driver.manage().window().maximize();
		
		driver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS);
										
		driver.get("https://www.baidu.com/duty/");
		
		//定义第一段文字
		WebElement Sting_Sected = driver.findElement(By.xpath("//*/div[@class='dwri_bule']/table/tbody/tr/td/p"));
		
		//定义第二段文字
		WebElement String_Second = driver.findElement(By.xpath("//*/div[@class='dwri_bule']/table/tbody/tr/td/ul/li[1]"));
		
		Actions action = new Actions(driver);
		action.clickAndHold(Sting_Sected).moveToElement(String_Second).perform();
		action.release();
		
	}

}

       在上面代码里定义了两个元素element1 和element2,element1是定位在最顶端的那段文字,element2是定义下面第一小点的文字。功能原理就是,找到顶端那个文字,然后按下鼠标不动,移动到element2然后释放鼠标,在实际过程中,会随机划取element1到element2直接的文字,划多少文字,没法确定。

我这边运行效果




  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值