How to Identify Xpath to click diff pages in google search | Selenium Forum
M
Posted on 30/05/2016
Objective: To click on the different page links (eg, 2, 3, 4, 5 ...) on google search.

I got stuck on identification of correct xpath. It is not clicking when i is incremented. Below is the code. Please advise.

public class seleniumtest {

public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.navigate().to("http://www.google.com");
driver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS);
driver.findElement(By.xpath("//*[@id='sb_ifc0']")).sendKeys("selenium");
driver.findElement(By.className("gsfi")).sendKeys(Keys.RETURN);

int i = 2;
while(true)
{
// i is incremented but not navigating to subsequent pages.
driver.findElement(By.xpath("//*[@id='nav']/tbody/tr/td["+i+"]/a")).click();
i++;

M
Replied on 31/05/2016

is it giving you any error?


M
Replied on 31/05/2016

yes, I am getting the below error. The code only clicks one page and then stops

Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"xpath","selector":"//*[@id='nav']/tbody/tr/td[2]/a"}
Command duration or timeout: 2.10 seconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.53.0', revision: '35ae25b', time: '2016-03-15 16:57:40'
System info: host: 'GG-PC', ip: '10.0.0.146', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_60'
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{applicationCacheEnabled=true, rotatable=false, handlesAlerts=true, databaseEnabled=true, version=46.0.1, platform=WINDOWS, nativeEvents=false, acceptSslCerts=true, webStorageEnabled=true, locationContextEnabled=true, browserName=firefox, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true}]
Session ID: 29f86c67-fd69-4cfb-a536-95428a9f943f
*** Element info: {Using=xpath, value=//*[@id='nav']/tbody/tr/td[2]/a}
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:206)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:158)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:678)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:363)
at org.openqa.selenium.remote.RemoteWebDriver.findElementByXPath(RemoteWebDriver.java:500)
at org.openqa.selenium.By$ByXPath.findElement(By.java:361)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:355)
at Selenium.seleniumtest.main(seleniumtest.java:24)
Caused by: org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"xpath","selector":"//*[@id='nav']/tbody/tr/td[2]/a"}
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.53.0', revision: '35ae25b', time: '2016-03-15 16:57:40'
System info: host: 'GG-PC', ip: '10.0.0.146', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_60'
Driver info: driver.version: unknown
at <anonymous class>.FirefoxDriver.prototype.findElementInternal_(file:///C:/Users/gaurav/AppData/Local/Temp/anonymous6408612921505044524webdriver-profile/extensions/fxdriver@googlecode.com/components/driver-component.js:10770)
at <anonymous class>.fxdriver.Timer.prototype.setTimeout/<.notify(file:///C:/Users/gaurav/AppData/Local/Temp/anonymous6408612921505044524webdriver-profile/extensions/fxdriver@googlecode.com/components/driver-component.js:625)


M
Replied on 31/05/2016

I also tried to use driver.findElement(By.xpath("//a[text()="+i+"]")).click();

still getting the error..

Exception in thread "main" org.openqa.selenium.WebDriverException: Element is not clickable at point (360, 517). Other element would receive the click: <div style="width: 1250px; height: 1586px; top: 166px;" class="flyr-o" id="flyr"></div>
Command duration or timeout: 86 milliseconds


M
Replied on 01/06/2016

where is this xpath pointing

[code:9e0my7nb]driver.findElement(By.xpath("//a[text()="+i+"]")).click();?
[/code:9e0my7nb]


M
Replied on 01/06/2016

This xpath is pointing to different pages depending on the value of i. Below is the complete code.

I am not figure out why is this not working. Please advice.

public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.navigate().to("http://www.google.com");
driver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS);
driver.findElement(By.xpath("//*[@id='sb_ifc0']")).sendKeys("selenium");
driver.findElement(By.className("gsfi")).sendKeys(Keys.RETURN);

int i = 2;
while(true)
{
driver.findElement(By.xpath("//a[contains(@href, '/search')][text()="+i+"]")).click();
i++;


M
Replied on 01/06/2016

Element is not clickable at point because the the the page number is at end .

You need to scroll the page or move the mouse point to end of the page and then click.
Refer Module 16 , Use MyListener.java

I have coded and it is working for me, attached the code , Download and run.

use:
EventFiringMouse mouse = new EventFiringMouse ( driver,myListener );
Locatable hoverItem = (Locatable) driver.findElement(By.xpath(".//*[@id='brs']/h3"));
Coordinates c= hoverItem.getCoordinates();
mouse.mouseMove(c);


M
Replied on 04/06/2016

I used listeners but still facing the same issue. Please see the code and response. Although it is locating the coordinates but still not clicking. Please advise.

//code
public static void main(String[] args) {
WebDriver web_driver = new FirefoxDriver();
EventFiringWebDriver driver = new EventFiringWebDriver(web_driver);
MyListener MyListener = new MyListener();
driver.register((WebDriverEventListener) MyListener);
driver.manage().window().maximize();
driver.navigate().to("http://www.google.com");
driver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS);
driver.findElement(By.xpath("//*[@id='sb_ifc0']")).sendKeys("selenium");
driver.findElement(By.className("gsfi")).sendKeys(Keys.RETURN);

int i = 2;
while(true)
{
EventFiringMouse mouse = new EventFiringMouse(driver, (WebDriverEventListener) MyListener);
Locatable bar = (Locatable) driver.findElement(By.xpath("//*[@id='brs']/h3"));
Coordinates c = bar.getCoordinates();
try{
mouse.mouseMove(c);
}

catch(Exception e1)
{

}
Point p = driver.findElement(By.xpath("//a[text()="+i+"]/span[@class='csb ch']")).getLocation();
System.out.println(p.x);
System.out.println(p.y);
driver.findElement(By.xpath("//a[text()="+i+"]/span[@class='csb ch']")).click();
i++;
driver.manage().timeouts().implicitlyWait(2,TimeUnit.SECONDS);
}

}}

// error
330
1666
350
1666
Exception in thread "main" org.openqa.selenium.WebDriverException: Element is not clickable at point (360, 509). Other element would receive the click: <div style="width: 1250px; height: 1586px; top: 166px;" class="flyr-o" id="flyr"></div>
Command duration or timeout: 94 milliseconds


M
Replied on 05/06/2016

use my above code and try


M
Replied on 05/06/2016

in your original code, Just put a wait in the while loop before clicking on the element.


while(true){
// i is incremented but not navigating to subsequent pages.
Thread.sleep(2000);
driver.findElement(By.xpath("//a[text()="+i+"]")).click();

i++;
}


M
Replied on 05/06/2016

Thank you very much. After giving Thread.sleep original code is working as expected.

Previously, I was trying with Implicit wait instead of thread.sleep, Can you tell why implicit wait did not work?

driver.manage().timeouts().implicitlyWait(2000,TimeUnit.MILLISECONDS);


M
Replied on 06/06/2016

don't use thread.sleep it make the selenium project unstable.


If you want to wait for particular element to load its better to use Explicit waits