gpt4 book ai didi

java - 无法使用 selenium webdriver 单击/鼠标悬停在元素上

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:48:28 25 4
gpt4 key购买 nike

1.转到此 URL :-> http://wallethub.com/profile/test_insurance_company/

2.在页面右侧,将鼠标悬停在星星上,然后点击第五颗星代码实际上应该 (1) 执行悬停操作,并且 (2) 确保当您将鼠标悬停在星星上方时,里面的星星会亮起,然后 (3) 单击第五颗星星。

下面的不行

 import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.firefox.internal.ProfilesIni;
import org.openqa.selenium.interactions.Actions;

public class Wallethub2 {

/**
* @param args
*/
public static void main(String[] args) {

ProfilesIni prof = new ProfilesIni();
FirefoxProfile fp = prof.getProfile("sel");

WebDriver driver=new FirefoxDriver(fp);

driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.get("http://wallethub.com/profile/test_insurance_company/");

// Xpaths of 5 stars
String star1="//div[@class='wh-rating-choices-holder']/a[contains(@href, '#')][1]";
String star2="//div[@class='wh-rating-choices-holder']/a[contains(@href, '#')][2]";
String star3="//div[@class='wh-rating-choices-holder']/a[contains(@href, '#')][3]";
String star4="//div[@class='wh-rating-choices-holder']/a[contains(@href, '#')][4]";
String star5="//div[@class='wh-rating-choices-holder']/a[contains(@href, '#')][5]";

WebElement srating1=driver.findElement(By.xpath(star1));
WebElement srating2=driver.findElement(By.xpath(star2));
WebElement srating3=driver.findElement(By.xpath(star3));
WebElement srating4=driver.findElement(By.xpath(star4));
WebElement srating5=driver.findElement(By.xpath(star5));


Actions builder = new Actions(driver);
builder.moveToElement(srating1).build().perform();
builder.moveToElement(srating1).build().perform();
builder.moveToElement(srating2).build().perform();
srating3.click();








}

}

最佳答案

如果要点击第五颗星应该是srating5.click()

或者,如果您的代码存在问题,请尝试使用:

Locatable hoverItem = (Locatable) driver.findElement(srating1);

Mouse mouse = ((HasInputDevices) driver).getMouse();

mouse.mouseMove(hoverItem.getCoordinates());

为第一个星星做悬停然后重复。

对于最后一项,你应该能够做你正在做的事情或尝试

Locatable clickItem = (Locatable)driver.findElement(srating3).getLocation();
clickItem.getCordinates(clickItem).click();

关于java - 无法使用 selenium webdriver 单击/鼠标悬停在元素上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20042286/

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