简体   繁体   中英

Mouse over Selenium Webdrive 2 Java

I am working on a public site. preview.harriscounty.org I am trying to mouse over ANY one of the many markers in the right pane, but I am not having success.

When you mouse over, a small popup occurs, and a "More Information" link is displayed. My goal is to click on that through mouse actions.

Below, I pasted code based on my research. I implemented in two different ways, but I am not getting any results. See if you can help crack this case. Much appreciated. BTW, There is a function called loadAll in the code, which simply selects All the markers, because I don't know which one I am selecting (I'm selecting any one of them). Just use the function pasted as-is. This avoids trying to mouse over an invisible marker (by first calling loadAll ). I also included Sleep method code, although noteworthy is that Sleep(2); is the same thing as Thread.sleep(2000);

STATIC MAIN METHOD CODE (EXECUTION STARTS HERE):

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.");
    }
}

I selected the option 'City of Sugar Land' only to reduce number of markers. First piece of code between dashed lines, first line selects all options and then deselects all options. Then chooses the required options. Include at your own judgement.

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

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

Below does the mouse over etc etc. Set the variable below to whatever value you want.

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

Okay this xpath below will give you all the elements so you can try and iterate trough them if you want (probably via an array list).

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

Alternatively, if you want to perform the hover then click action this needs to happen in 2 steps:

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

And then click on the info box:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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