简体   繁体   English

将鼠标悬停在Selenium Webdrive 2 Java上

[英]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. Preview.harriscounty.org我试图将鼠标悬停在右窗格中的许多标记中的任何一个上,但是我没有成功。

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). 顺便说一句,代码中有一个名为loadAll的函数,它仅选择所有标记,因为我不知道我要选择哪个标记(我正在选择其中的任何一个)。 Just use the function pasted as-is. 只需按原样粘贴函数即可。 This avoids trying to mouse over an invisible marker (by first calling loadAll ). 这样可以避免尝试将鼠标悬停在不可见的标记上(首先调用loadAll )。 I also included Sleep method code, although noteworthy is that Sleep(2); 我还包括了Sleep方法代码,尽管值得注意的是Sleep(2); is the same thing as Thread.sleep(2000); 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). 好的,下面的xpath将为您提供所有元素,因此您可以根据需要尝试遍历它们(可能通过数组列表)。

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

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

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