先贴上代码
package com.u;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
public class Test_2 {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver","src/main/resources/chromedriver");
ChromeDriver webDriver=new ChromeDriver();
webDriver.get("https://baidu.com");
WebElement xueshu = webDriver.findElementByName("tj_trxueshu");//百度首页的学术超链接
WebElement kw = webDriver.findElementById("kw");//百度首页的输入框
Actions actions = new Actions(webDriver);
actions.clickAndHold(xueshu).moveByOffset(kw.getLocation().getX()-xueshu.getLocation().getX(),kw.getLocation().getY()-xueshu.getLocation().getY()).perform();
}
}
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.xxx.test</groupId>
<artifactId>selenium-demo</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.10.0</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-chrome-driver</artifactId>
<version>3.9.1</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
</dependency>
</dependencies>
</project>
环境:JDK8 + MAC
我本意是想让这段代码完成 “将学术这个超链接拖到百度的搜索栏中”,但是失败了,这个超链接显示在我的 IDEA 界面上了,有点心塞,能给点指导吗?