emacs
emacs

Reputation: 101

click link selenium web driver works for ie not firefox

I can't for the life of me figure out what is going on. the code is simple:

//WebDriver driver = new InternetExplorerDriver();  
//WebDriver driver = new FirefoxDriver();
driver.get("http://www.yahoo.com");
driver.findElement(By.xpath("//*[@id='pa-u_14782488-bd']/a/span[2]")).click();

I use either ff or ie driver. but last 2 line of code is same. works for ie, but not ff. funny thing is i'm getting the xpath from ff firebug so xpath is correct for ff. ff version 7.0.1. Its just the Mail link on the left column of yahoos site. Why is this so hard?

Upvotes: 6

Views: 6592

Answers (3)

Amith
Amith

Reputation: 7018

As Slanec mentioned these kind of sites use dynamic id's,so a better option in the above specified case would be to use "title" attribute,which has lesser probability of changing.. if you want to go with xpath,this will work,

driver.findElement(By.xpath("//*[@title='Mail']")).click();

Even better option will be to use link text,because it works the same way as the user manually would click...

driver.findElement(By.linkText("MAIL")).click();

Upvotes: 2

Baz1nga
Baz1nga

Reputation: 15579

dont use xpath search.. its highly unstable and doesnt help your case at all. Also your selenium code is now tightly coupled with the markup and any changes to the markup like introduction of a container (i.e div) will fail the test.

you could use scoping to achieve something similar. example:

var container=driver.findElement(By.xpath("//*[@id='pa-u_14782488-bd']"));

var spans=container.findElements(By.tagName("span"));

spans[1].Click();

Also I dont understand how this works though, cos you have a link and for I am guessing for styling purposes you have two spans within it but clicking on either of them should still trigger the same action as clicking on the click correcT?or am I missing something??

It might just be the case that firefox is handing your events in an incorrect way.

Upvotes: 0

prestomanifesto
prestomanifesto

Reputation: 12816

Selenium has a hard time sometimes with elements embedded within link (<a>) elements. Try your code without the last part of the XPath. So:

//*[@id='pa-u_14782488-bd']/a

Make sure you also post what version of Selenium you are using so others can give you more detailed help.

Upvotes: 0

Related Questions