鼠标悬停在Selenium Webdrive 2 Java上

时间:2016-08-18 14:39:22

标签: java selenium mouse

我正在公共网站上工作。 preview.harriscounty.org 我试图将鼠标悬停在右侧窗格中的任何一个标记上,但我没有成功。

鼠标悬停时,会弹出一个小弹出框,然后显示一个"更多信息"链接显示。我的目标是通过鼠标操作点击它。

下面,我根据我的研究粘贴了代码。我以两种不同的方式实施,但我没有得到任何结果。看看你是否可以帮助破解这种情况。非常感激。顺便说一句,代码中有一个名为loadAll的函数,它只选择所有标记,因为我不知道我选择哪一个(我选择其中任何一个)。只需使用按原样粘贴的功能即可。这样可以避免尝试将鼠标悬停在不可见标记上(首先调用loadAll)。我还包括Sleep方法代码,但值得注意的是Sleep(2);Thread.sleep(2000);

相同

静态主要方法代码(此处开始执行):

driver = new FirefoxDriver();
WebDriver driver;
driver.get("http://preview.harriscountyfws.org/");

//Select All Sites Agencies (followed by Close button):
loadAll(driver);

JOptionPane.showMessageDialog(frame,
            "Locating and mousing over to a Rainmarker\n\n    ");

WebElement element = driver.findElement(By.className("rainMarker"));

Actions actions = new Actions(driver);
actions.moveToElement(element).click().build().perform();
Sleep(5);
JOptionPane.showMessageDialog(frame, "Did you see anything?\n\n    ");

Locatable hoverItem = (Locatable) element;
Mouse mouse = ((HasInputDevices) driver).getMouse();
mouse.mouseMove(hoverItem.getCoordinates());
Sleep(5);
JOptionPane.showMessageDialog(frame, "Did you see anything?\n\n    ");




public static void loadAll(WebDriver driver) {
    WebElement we, listbox_element;
    we = driver.findElement(
            By.xpath("//div[@id='searchDiv']//span[@aria-owns='ddlRegion_listbox']//span[@class='k-select']"));
    we.click();

    new WebDriverWait(driver, 3, 100).until(ExpectedConditions.visibilityOfElementLocated(
            By.id("regionSelectPopup")));

    listbox_element= driver.findElement(
            By.xpath("//div[@id='regionSelectPopup']//label[.='ALL']/preceding-sibling::input[@type='checkbox']"));

    listbox_element.click();
    we = driver.findElement(By.xpath("//*[@id=\"regionSelectPopup\"]/div[2]/input"));  // CLOSE BUTTON
    we.click();
    Sleep(1);
}


public static void Sleep(int i)
{
    try
    {
        if (i<100)
        Thread.sleep(i * 1000);
        else Thread.sleep(i);
    }catch(InterruptedException ie)
    {
        //Log message if required.
        System.out.println("Unexpected error in sleep method.");
    }
}

2 个答案:

答案 0 :(得分:0)

我选择了“Sugar Land&#39;只是为了减少标记的数量。虚线之间的第一段代码,第一行选择所有选项,然后取消选择所有选项。然后选择所需的选项。包括你自己的判断。

--------------------------------------

String location = "City of Sugar Land";
            driver.findElement(By.xpath("//div[@id='regionSelectPopup']//label[.='ALL']/preceding-sibling::input[@type='checkbox']")).click();

    driver.findElement(By.xpath("//div[@id='regionSelectPopup']//label[.='ALL']/preceding-sibling::input[@type='checkbox']")).click();

    driver.findElement(By.xpath("//div[@id='regionSelectPopup']//label[.='"+ location +"']/preceding-sibling::input[@type='checkbox']")).click();
    -----------------------------------
  

鼠标悬停在下面等等。将下面的变量设置为你想要的任何值。

String markerValue = "1.32";

        WebElement rainMarker = driver.findElement(By.xpath("//div[@id='mapDiv']/div/div/div/div[not(contains(@style,'display'))]"
                + "//a[@class='MapPushpinBase']/div[@class='rainMarker'][.='"+ markerValue +"']"));

        Actions actions = new Actions(driver);
        actions.moveToElement(rainMarker).perform();

        new WebDriverWait(driver, 3, 100).until(ExpectedConditions.visibilityOf(rainMarker));

        driver.findElement(By.xpath("//div[@id='infoBox'][contains(@style,'visibility: visible')]/a")).click();

答案 1 :(得分:0)

好的,下面的xpath将为您提供所有元素,以便您可以尝试通过它们进行迭代(可能是通过数组列表)。

//div[@id='mapDivContainer']//div[@class='rainMarker']

或者,如果要执行悬停,则单击操作,这需要分两步进行:

Actions gotoElement= new Actions(driver);
hoverElement= driver.findElement(By.xpath("//div[@id='mapDivContainer']//div[@class='rainMarker']")
gotoElement.moveToElement(hoverElement).perform();

然后点击信息框:

driver.findElement(By.xpath("//div[@id='//div[@id='mapDivContainer']//div[@id='infoBox']")
gotoElement.moveToElement(clickInfoBox).click().perform();