Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 463
emacs Avatar asked Nov 17 '11 02:11

emacs


People also ask

Does Selenium WebDriver support Firefox browser?

Up to Selenium 2.53 versions, Firefox was the native browser for Selenium WebDriver and the user did not have to download any additional package or driver executable for launching Firefox browser. But, from Selenium 3.0, you need to download the Gecko driver which will interact with the Firefox browser.

How to interact with links in Selenium WebDriver?

Selenium WebDriver provides two different identifiers to interact with links. linktext () identifier matches exact text associated with the link web element. So, to click on above-mentioned link code will be: partialLinktext () identifier matches partial text associated with the link web element. It can be considered as a contains method.

Why selenium is sending the click but browser is not?

I think selenium is sending the click but browser (IE/FF/chrome) are not ready to process it. May be its binding to JS is not done or something like that.

How to install Mozilla geckodriver in Selenium WebDriver?

Step 1: Navigate to the official Selenium website. Under third-party drivers, one will find all the drivers. Just click on the Mozilla GeckoDriver documentation as shown below. Step 2: After that, check the latest supported platforms of GeckoDriver versions in the documentation and click on GeckoDriver releases as shown below:


1 Answers

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();
like image 154
Amith Avatar answered Oct 04 '22 06:10

Amith