gpt4 book ai didi

java - 鼠标悬停操作在演示网站的 selenium webdriver 中不起作用

转载 作者:行者123 更新时间:2023-12-02 03:23:44 25 4
gpt4 key购买 nike

我已经尝试过以下代码。

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();

}
}

enter image description here

我需要点击女性类别中的“T 恤”链接。无法使用鼠标悬停操作单击链接。

最佳答案

使用下面的代码:

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

希望这对您有帮助。

关于java - 鼠标悬停操作在演示网站的 selenium webdriver 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39268326/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com