1

I am trying to get a WebElement with Selenium:

driver.findElement(By.xpath("//input[@name='j_username']"))

But Selenium says: "Unable to find element with XPath ...". The XPath is valid, I proofed it with FirePath. But the input element has the following invalid code:

<input size="10" type="text" name="j_username" maxlength="8">

I can't change the html-file, despite the fact is there any solution to get the webElement? Thanks in advance!

Janus
  • 309
  • 1
  • 5
  • 18
  • If you execute that XPath query in Firebug (by going into the Console, type in `$x("//input[@name='j_username']")`) does it find anything? Is the element in a popup window or iframe? Is it visible on the page to begin with or does some action cause it to become visible? – Arran Sep 27 '12 at 14:18
  • If I execute the query the consol responds: [input]. The element is not in a popup window or iframe. It is visible all the time. – Janus Sep 27 '12 at 14:45

3 Answers3

1

try select element with css selector. and also verify in firepath(firebug addon that element is located properly). so your css selector be something like

input[name='j_username']

2nd approach is to use internal firebug mechanism for finding xPaths of elements. See screen attached below enter image description here

After these manipulations driver shoulda handle element properly.

eugene.polschikov
  • 7,254
  • 2
  • 31
  • 44
  • No, it does not work. I tried the variant with an absolute path and the version with the css-selector. FirePath and FireBug get the elements, but selenium throws every the exception, that it can not find the elements. – Janus Oct 01 '12 at 09:00
0

Well I will suggest adding an id to your html code -

<input id="j_username"size="10" type="text" name="j_username" maxlength="8">

and findElement by id -

driver.findElement(By.id("j_username"));

I have faced similar issues with xpath(borwser issues??) but id never fails for me. ;)

By the way I feel your code should be -

driver.findElement(By.xpath(".//*[@name='j_username']"));
some_other_guy
  • 3,364
  • 4
  • 37
  • 55
  • One of my problems was, that I'm not able to alter the HTML code of the site. I tried your versions, but the result is the same: with XPath and Firebug no problem, but Selenium does not find the elements. Meanwhile I think there is no solution with Selenium :( Thanks for your help! – Janus Oct 04 '12 at 07:00
0

The best solution is to find out what selenium is doing wrong, but without a URL or sample page to test on it's a little hard. Is there anyway you could dump the HTML into a jsfiddle? If there is do that and paste the url into the question and I'm sure someone can find a solution.

If not however, another way to get the results is to do it with jQuery. If firebug is picking it up but not selenium, then there's no reason why jQuery wouldn't get it. Here's how to go about doing that if needed:

Step 1: Is jQuery already present on the page? If so then you don't need to do this bit, otherwise you will need to add it yourself by using driver.executeScript(addjQueryScript) where the script does something like this.

Step 2: call WebElement input = driver.executeScript(elementSelector); where the elementSelector script would be something like \"return $('input[name=\"j_username\"]')\");

My jQuery's not so good, but I believe that should work...

Best of luck!

Community
  • 1
  • 1
Jamey
  • 813
  • 1
  • 12
  • 27