Tharindu Kandegedara
Tharindu Kandegedara

Reputation: 91

unable to locate href element within a mouse over area - Selenium web driver

I tried to locate a link "Sign in" located in the window which appeared when mouse move on to "Mail" link on Yahoo. I can get the xpath using firebug. but when i used it in the script, it doesn't work.

enter image description here

HTML snippet :

<a id="yui_3_18_0_4_1456816269995_943" class="C($menuLink) Fw(b) Td(n)" 
    data-ylk="t3:usr;elm:btn;elmt:lgn;" data-action-outcome="lgn" 
    href="login.yahoo.com/config/…; data-rapid_p="23">Sign in</a> 

I tried this in my code within main method,

    WebElement element = driver.findElement(By.id("uh-mail-link"));
    Actions action = new Actions(driver);
    action.moveToElement(element).build().perform();
    driver.findElement(By.id("yui_3_18_0_4_1456804882382_929")).click();

id selecter;

driver.findElement(By.id("yui_3_18_0_4_1456804882382_929")).click();

It prompts this error;

Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"id","selector":"yui_3_18_0_4_1456804882382_929"}
Command duration or timeout: 17 milliseconds

Can we locate it using id of appeared window ".//*[@id='yui_3_18_0_4_1456804882382_919']" and linkText "Sign in", or are there any other methods to locate it in the script.

Upvotes: 1

Views: 666

Answers (2)

har07
har07

Reputation: 89285

You're supposed to pass just id to By.id(), not an XPath expression :

driver.findElement(By.id("yui_3_18_0_4_1456804882382_929")).click();

or use By.xpath() instead of By.id() if you need to find the element by XPath expression, for example, by using combination of id and link text to locate the target element.

UPDATE :

You can filter element by its text content and id like so :

//a[@id='yui_3_18_0_4_1456816269995_943' and .='Sign in']

Upvotes: 1

Marțiș Paul
Marțiș Paul

Reputation: 121

Have you tried putting 2 functions ? 1 for mouse over and 1 for actual clicking ? I have a sample in Java if you need.

public void mouseOver(String xPath){ 
        Actions action = new Actions(driver);
        WebElement element = driver.findElement(By.xpath(xPath));
        action.moveToElement(element).moveToElement(driver.findElement(By.xpath(xPath))).click().build().perform();
        Thread.sleep(500); //too actualy see if is it performs
}
public void click(String xpath) {
        driver.findElement(By.xpath(xpath)).click();
}

You can change the searching method from xpath to id, or whatever you need. Be aware that the mouseOver function has a different xpath/id to send from the click method because , you mouseOver on first button, and you click the other link that will apear after.

Hope it helps.

Upvotes: 0

Related Questions