Unable to locate from google search result

516 views
Skip to first unread message

Amritesh

unread,
Feb 28, 2014, 8:58:09 AM2/28/14
to seleniu...@googlegroups.com
Hi All,
From google search result I want to to click a link. I find the element by xpath but its unable to locate.
For the same i written following code:

WebDriver driver=new FirefoxDriver();
List<WebElement> btns=driver.findElements(By.tagName("button"));
for(WebElement btn : btns)
System.out.println(btn.getAttribute("aria-label"));
driver.findElement(By.id("gbqfq")).sendKeys("naukri.com");
driver.findElement(By.id("gbqfb")).click();
driver.findElement(By.xpath("html/body/div[1]/div[3]/div[2]/div[6]/div[1]/div[4]/div/div[2]/div[2]/div/ol/li[1]/div/div/h3/a/em[1]")).click();
But its reflect following Error in console:

 Unable to locate element: {"method":"xpath","selector":"html/body/div[1]/div[3]/div[2]/div[6]/div[1]/div[4]/div/div[2]/div[2]/div/ol/li[1]/div/div/h3/a/em[1]"}
Command duration or timeout: 29 milliseconds

Plz solve it...

Shawn McCarthy

unread,
Feb 28, 2014, 10:47:37 AM2/28/14
to seleniu...@googlegroups.com
This selector : By.xpath("html/body/div[1]/div[3]/div[2]/div[6]/div[1]/div[4]/div/div[2]/div[2]/div/ol/li[1]/div/div/h3/a/em[1]") is horrible lol. So much can go wrong there. Are there any unique elements closer to the em[1] ?

Ravindra

unread,
Feb 28, 2014, 7:19:21 PM2/28/14
to seleniu...@googlegroups.com
Hey Amritesh,
                      The Xpath you are trying to use is not recommended. 
Why ?? - There are two kind of Xpath 1) Absolute 2) Relative
 Absolute Xpath: works as your html code hierarchy. It start with the the root element and end with the desired descendant element 
In Case of Relative Xpath we directly hit the desired element with some unique attribute of html tag
 and why Absolute Xpath is not recommended is because if tomorrow something is changed in you html code then it will fail to locate that element

try to create your own xpath by taking some unique attribute.


Toast!!
~Ravindra

amritesh kumar

unread,
Mar 1, 2014, 2:15:41 AM3/1/14
to seleniu...@googlegroups.com
Hi Ravindra,
Thanks for your reply.
But I also used with different type of xpath like, 

driver.findElement(By.xpath("//a[@href='http://www.naukri.com/']")).click();

But didn't able to get result....
HTML code for the same is:

<h3 class="r">
<a onmousedown="return rwt(this,'','','','1','AFQjCNFTGjaolwPb8F_WZ-WMObomPpJrog','','0CDYQFjAA','','',event)" href="http://www.naukri.com/">
<em>Naukri</em>
.
<em>com</em>
– Jobs – Jobs in India – Recruitment – Job Search
<b>...</b>
</a>
</h3>

If u have solution, plz help...


--
You received this message because you are subscribed to a topic in the Google Groups "Selenium Users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/selenium-users/gx70j9ufFjI/unsubscribe.
To unsubscribe from this group and all its topics, send an email to selenium-user...@googlegroups.com.
To post to this group, send email to seleniu...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/b811da5c-b613-4da8-bcb2-3fffaef9894b%40googlegroups.com.

For more options, visit https://groups.google.com/groups/opt_out.



--
Thanks & Warm Regards,
Amritesh Kumar

Ravindra

unread,
Mar 2, 2014, 10:17:35 AM3/2/14
to seleniu...@googlegroups.com
Hmm, So these are the scenarios where Wait comes in picture 

There is nothing wrong with your code, i tried with both of the xpath you mentioned above and its working as expected.

Just put a wait (for 6 to 7 second )after statement driver.findElement(By.id("gbqfb")).click(); and it will work like hell :)

Toast!!
~Ravindra

Saravanan Ravi

unread,
Mar 5, 2014, 8:27:58 AM3/5/14
to seleniu...@googlegroups.com
Yes, putting Thread.sleep(3000) code after clicking the Search button, will resolve your issue.
 
Regards,
Saravanan R

praga praga

unread,
Mar 7, 2015, 11:22:55 PM3/7/15
to seleniu...@googlegroups.com
@Saravana Ravi


It really worked.Thanks:)
Reply all
Reply to author
Forward
0 new messages