繁体   English   中英

将鼠标悬停在Selenium Webdrive 2 Java上

[英]Mouse over Selenium Webdrive 2 Java

我正在公共场所工作。 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.");
    }
}

我选择“糖城之城”选项只是为了减少标记数。 虚线之间的第一段代码,第一行选择所有选项,然后取消选择所有选项。 然后选择所需的选项。 包括您自己的判断。

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

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

好的,下面的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();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM