-1

All:

Does Google some how prevent searching of elements by id using Xpath?

    WebDriver wbdriver = new HtmlUnitDriver();


    wbdriver.get("https://www.google.co.in/?gfe_rd=cr&ei=oB5BVNXnOKzV8gePnYDQBA");

    WebElement element = wbdriver.findElement(By.xpath("//form[@id='gbqf']"));

I found other tags by name or tagname. However, I've been trying for a really long time to find by Xpath using id.

Does google have restrictions that prevents the aforementioned code from working?

Thanks in advance

Update with Answer

I used FirefoxDriver instead of HtmlUnitDriver

WebDriver driver= new FirefoxDriver();

crazyTech
  • 1,379
  • 3
  • 32
  • 67
  • 3
    No. However you've got two problems: not using a real browser and using a complicated site. – Arran Oct 17 '14 at 13:59
  • r u stating that the problem is because I'm using HtmlUnitDriver ? – crazyTech Oct 17 '14 at 14:05
  • 1
    More of a suggestion to try it in Chrome, before assigning the problem to Google/Selenium. – Arran Oct 17 '14 at 14:07
  • If you're wanting to use a headless browser I would really recommend the PhantomJS/Ghostdriver package over HTMLUnitDriver. The later isn't without its faults when there are JavaScript errors on a page mind. – Mark Rowlands Oct 17 '14 at 14:22
  • What error do you get that makes you say it does not work? FWIW, I prefer to use XPath only if the other methods cannot do what I want to do or can't do it without convoluted code. Here I'm wondering why not just search by id... – Louis Oct 17 '14 at 16:43

1 Answers1

2

XPath should work if Id is working to find elements. May be you got the Xpath wrong. Here is what I found as XPath from Google Search page:

//*[@id="gbqfq"]

Hope this helps!

Kishore Banala
  • 946
  • 1
  • 8
  • 12