我已尝试过以下代码。
public class FindingMultipleElements {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.manage().window().maximize();
driver.navigate().to("http://automationpractice.com/index.php");
Actions act = new Actions(driver);
WebElement women = driver.findElement(By.xpath("//*[@id='block_top_menu']/ul/li[1]/a"));
//women.click();
Point p1 = women.getLocation();
int x = p1.getX();
int y = p1.getY();
System.out.println("X:"+x+" Y:"+y);
act.moveByOffset(x, y).click(driver.findElement(By.linkText("T-shirts"))).build().perform();
}
}
我需要点击女性类别中的“T恤”链接。无法使用鼠标悬停操作单击链接。
答案 0 :(得分:2)
使用以下代码:
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.manage().window().maximize();
driver.navigate().to("http://automationpractice.com/index.php");
driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
WebElement women = driver.findElement(By.cssSelector("ul>li:nth-child(1)>a[title='Women']"));
Actions builder = new Actions(driver);
builder.moveToElement(women).perform();//this will hover to women
Thread.sleep(1000);//avoid using this type of wait. wait using until.
driver.findElement(By.cssSelector("ul>li:nth-child(1)>a[title='T-shirts']")).click();//this will click on t-shirt
希望这会对你有所帮助。
答案 1 :(得分:0)
您已提供X
和Y
坐标,那么为什么还提供driver.findElement(By.linkText("T-shirts"))
?尝试没有它,它会工作。
答案 2 :(得分:0)
没有元素By.linkText(“T恤”)
Actions act = new Actions(driver);
WebElement womenLink = driver.findElement(By.xpath("//a[@title='Women']"));
act.moveToElement(womenLink).click().build().perform();
WebElement tshirtLink = driver.findElement(By.xpath("//[@class='sfHoverForce']//a[@title='T-shirts']"));
act.moveToElement(tshirtLink).click().build().perform();