Header Ads

Why Xpath Is Slower in Browsers Specially In Internet Explorer?

This question is asked in a forum. All most all the selenium developers try to avoid to use xpath. They use only in one condition if at tall they don't find any other locator correctly.
I was reading articles on this, and I found

I request all of you to go through these links.
So it is not good to blame xpath. It is the version of the browsers and how they are rendering the Xpath.The report clearly shows that IE-9 onwords xpath is not that slow.The performance difference is slightly in favor of CSS for non-IE browsers and slightly in favor of XPath for IE browsers.
I got one more example where xpath performed slow:



import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class qtpseleium_example {

 public static void main(String[] args) throws Exception {
  WebDriver driver=new FirefoxDriver();
  driver.get("http://qtpselenium.com/"); 
  driver.manage().window().maximize();
  driver.findElement(By.xpath("//*[@src='http://qtpselenium.com/images/MemberLogin_Button_R.jpg']")).click();
  driver.findElement(By.xpath("//a[@href='http://qtpselenium.com/login/seleniumLogin.php']/img")).click(); 
  driver.findElement(By.xpath("//input[@name='email1']")).sendKeys("subhasis.bhatt@xxxx");
  driver.findElement(By.xpath("//input[@name='password']")).sendKeys("xxxx");
  driver.findElement(By.xpath("//input[@type='submit']")).click();
  Thread.sleep(10000);
  //driver.findElement(By.xpath("//div[@class='box1']/ul/li[2]/a[contains(text(),'Selenium Training')]")).click();
  driver.findElement(By.xpath("//a[contains(@href,'selenium/module11')]/img")).click();
  driver.findElement(By.xpath("//a[text() = 'Part1']")).click();
  //doesn't work--String text=driver.findElement(By.xpath("//a[contains(text(),'Part 1')]")).click();
  //doesn't work--String text=driver.findElement(By.xpath("//a[contains(@onclick,'module11_1.php')]")).click();
  //doesn't work--String text=driver.findElement(By.xpath("//body[a[position()=1 and contains(text(),'Part 1')]]")).click();

 }

}

But when it was executed it gave:
ERROR:

Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"xpath","selector":"//a[text() = 'Part1']"}
Command duration or timeout: 170 milliseconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.46.0', revision: '87c69e2', time: '2015-06-04 16:17:10'
System info: host: 'BUBU', ip: '10.0.0.5', os.name: 'Windows 8.1', os.arch: 'amd64', os.version: '6.3', java.version: '1.8.0_45'
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{applicationCacheEnabled=true, rotatable=false, handlesAlerts=true, databaseEnabled=true, version=39.0, platform=WINDOWS, nativeEvents=false, acceptSslCerts=true, webStorageEnabled=true, locationContextEnabled=true, browserName=firefox, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true}]
Session ID: c15688ec-f6d8-4cb2-b3bd-b5ecc29f2094
*** Element info: {Using=xpath, value=//a[text() = 'Part1']}
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:204)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:156)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:605)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:358)
at org.openqa.selenium.remote.RemoteWebDriver.findElementByXPath(RemoteWebDriver.java:455)
at org.openqa.selenium.By$ByXPath.findElement(By.java:358)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:350)
at Module13.qtpseleium_example.main(qtpseleium_example.java:22)
Caused by: org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"xpath","selector":"//a[text() = 'Part1']"}
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.46.0', revision: '87c69e2', time: '2015-06-04 16:17:10'
System info: host: 'BUBU', ip: '10.0.0.5', os.name: 'Windows 8.1', os.arch: 'amd64', os.version: '6.3', java.version: '1.8.0_45'
Driver info: driver.version: unknown
at .FirefoxDriver.prototype.findElementInternal_(file:///C:/Users/SUBHAS~1/AppData/Local/Temp/anonymous2877297415601679170webdriver-profile/extensions/fxdriver@googlecode.com/components/driver-component.js:10299)
at .FirefoxDriver.prototype.findElement(file:///C:/Users/SUBHAS~1/AppData/Local/Temp/anonymous2877297415601679170webdriver-profile/extensions/fxdriver@googlecode.com/components/driver-component.js:10308)
at .DelayedCommand.prototype.executeInternal_/h(file:///C:/Users/SUBHAS~1/AppData/Local/Temp/anonymous2877297415601679170webdriver-profile/extensions/fxdriver@googlecode.com/components/command-processor.js:12282)
at .DelayedCommand.prototype.executeInternal_(file:///C:/Users/SUBHAS~1/AppData/Local/Temp/anonymous2877297415601679170webdriver-profile/extensions/fxdriver@googlecode.com/components/command-processor.js:12287)
at .DelayedCommand.prototype.execute/<(file:///C:/Users/SUBHAS~1/AppData/Local/Temp/anonymous2877297415601679170webdriver-profile/extensions/fxdriver@googlecode.com/components/command-processor.js:12229) error.





Now when we put more wait between lines

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class qtpseleium_example {

 public static void main(String[] args) throws Exception {
  WebDriver driver=new FirefoxDriver();
  driver.get("http://qtpselenium.com/"); 
  driver.manage().window().maximize();
  driver.findElement(By.xpath("//*[@src='http://qtpselenium.com/images/MemberLogin_Button_R.jpg']")).click();
  Thread.sleep(5000);
  driver.findElement(By.xpath("//a[@href='http://qtpselenium.com/login/seleniumLogin.php']/img")).click();
  Thread.sleep(5000);
  driver.findElement(By.xpath("//input[@name='email1']")).sendKeys("subhasis.bhatt@xxxx");
  driver.findElement(By.xpath("//input[@name='password']")).sendKeys("xxxx");
  driver.findElement(By.xpath("//input[@type='submit']")).click();
  Thread.sleep(5000);
  //driver.findElement(By.xpath("//div[@class='box1']/ul/li[2]/a[contains(text(),'Selenium Training')]")).click();
  Thread.sleep(5000);
  driver.findElement(By.xpath("//a[contains(@href,'selenium/module11')]/img")).click();
  Thread.sleep(5000);
  driver.findElement(By.xpath("//a[text() = 'Part1']")).click();
  Thread.sleep(5000);
  String text=driver.findElement(By.xpath("//a[contains(text(),'Part 1')]")).click();
  Thread.sleep(5000);
  String text=driver.findElement(By.xpath("//a[contains(@onclick,'module11_1.php')]")).click();
  Thread.sleep(5000);
  String text=driver.findElement(By.xpath("//body[a[position()=1 and contains(text(),'Part 1')]]")).click();

 }

}


It started working. So it is a proof that xpath worked when given more time.
Powered by Blogger.