I get the following error:
com.thoughtworks.selenium.SeleniumException: ERROR: Invalid xpath [2]: //input[#value='Accedi']
at com.thoughtworks.selenium.HttpCommandProcessor.throwAssertionFailureExceptionOrError(HttpCommandProcessor.java:112)
at com.thoughtworks.selenium.HttpCommandProcessor.doCommand(HttpCommandProcessor.java:106)
at com.thoughtworks.selenium.DefaultSelenium.click(DefaultSelenium.java:193)
The XML part of the file is:
<p>
<div><input class="login button" type="submit" value="Accedi" /></div>
</p>
My Java code is:
selenium.click("//input[#value='Accedi']");
I have tried to change single quotes into double quotes, changed xpath to:
/html/body/div[3]/div[2]/form/p[4]/div/input[#value="Accedi"]
Which I got from a tool.
Why are all the XPaths that I have tried wrong?
Can you make sure few things:
Can you make sure the Xpath evaluation is returning just a single object which you want to click.
If you are able to identify your object uniquely, then can you try clicking using below suggestions?
Can you try this Xpath : //input[contains(#class,'login button')] or
//input[contains(#value,'Accedi')]
If you still facing the issue, can you please post the Selenium
Version and the browser you are using. If you are using chrome or IE,
also specify the driver version for the same.
Related
I am on this website and I am trying to click on Batch search using the selenium webDriver:
https://metlin.scripps.edu/landing_page.php?pgcontent=mainPage
The HTML code for the link is:
<a href="landing_page.php?pgcontent=batch_search" style="color:white; font-weight:bold; font-size:13px">Batch Search </a >
My attempted solution is:
driver3.findElement(By.linkText("Batch Search")).click();
However, this doesn't seem to work.
Any ideas?
linkText is an exact match but there's an extra space character after "Batch Search" that's not present in your query so it won't match.
Either fix the search query or use partialLinkText.
I like to use css selectors, which would be:
String selector = "a[href=\"landing_page.php?pgcontent=batch_search\"]";
driver3.findElement(By.cssSelector(selector)).click()
I have this in the HTML:
<textarea name="comment" class="form-control" rows="3" id="textarea_1160688690910416779_2159935466"></textarea>
I want to interact with id=textarea_, but the numbers are constantly changing after the "_". To solve this, I used this code:
driver.findElement(By.xpath("[starts-with(#id, 'textarea')")).sendKeys(comment);
However I am getting the error:
org.openqa.selenium.NoSuchElementException: Unable to locate element:
Use
driver.findElement(By.xpath("//textarea[starts-with(#id,'textarea_')]")).sendKeys(comment);
The reason you're getting this error is that you need to add '//' before your current XPath.
You could either use starts-with, this way:
("//textarea[starts-with(#id, 'textarea_')]")
You could also give 'contains' a try:
("//textarea[contains(#id, 'textarea_')]")
In both ways, you could use //*[... instead of textarea, for more cases
Your Xpath is not a valid Xpath.
It should contain a path and tag name
and it should be balanced (your [ is not closed)
So, if your tag is, textarea, and if it is at top: use /textarea
if not at top, use //textarea
This gives: "//textarea[starts-with(#id, 'textarea')]"
To use with selenium, you could also read that: JAVA - How to use xpath in selenium
and that: Webdriver findElements By xpath
Well not to mention that I'm new on using the selenium web driver, I'm trying to automatize a mailbox so I can log myself in and to send a message, the thing is that I download the firebug plugin for Mozilla, that is the browser I'm currently working with, when I'm trying to find the element for the log in and the password everything goes well, because I do it by their name.
input class="_nb-input-controller" type="text" **name="login"** autocorrect="off" autocapitalize="off" value=""
input class="_nb-input-controller" type="password" **name="passwd"** value=""
but as soon as im at the mailbox entry the identificator changes it, i go to compose link and this is the code that shows up:
a class="b-toolbar__item b-toolbar__item_compose js-toolbar-item-compose daria-action" title="Compose (w, c)" href="#compose" data-action="compose.go" data-params="toolbar=1&toolbar.button=compose"
Since I don't find any method that could help on this, a friend came and told me about the xpath which was:
/html/body/div[2]/div/div[5]/div/div[3]/div/div[2]/div/div/div/div[2]/a[2]
by doing this selenium did find the element and I could click on the icon,
but again.. when I'm trying to get into the "TO" prompt so I can write inside
it is not finding the element, not by xpath, or anything is there a way or formula to get elements or translate them into a way that selenium can find them???
oh i see, i re try with the element and i noticed that the one i wanted was showing as hidden using firebug so i just try the next one up and the web driver could locate the item, it was a simple app to send an email the prompt that i wanted to locate was the 'TO' field. and thanks guys for giving me the correct way of using the Xpath property
Having locators such as :
/html/body/div[2]/div/div[5]/div/div[3]/div/div[2]/div/div/div/div[2]/a[2]
is generally a bad idea. If you want to build a stable test I would suggest you stay away from such.
Now I see you have attributes like #name and #class. XPath is provides you with a way to reach these elements directly by pointing the identifiers instead of the whole path to the element.
You can try something like:
//input[#name='login']
//input[#name='password']
More info at: http://www.w3schools.com/xsl/xpath_intro.asp
I have to test a dynamic app using a button like that:
<button class="btn btn-primary btn-mini" ng-click="addAnswer(question)" ng-show="question.editing">Add Answer</button>
I was trying to locate button with that but I failed
getDriver().findElement(By.xpath("//img [#ng-click='addAnswer(question)']")).click();
using css
List AddAnswerBtn =
getDriver().findElements(By.className("btn-primary"));
AddAnswerBtn.get(0).click();
What is the correct line to locate it? I don't want to locate it using class and I wants to use Xpath to locate button location.
Your example shows it is a button, but your XPath is looking for an image:
//img[#ng-click='addAnswer(question)']
The above is what you are using, with img as the element type.
Change this to button.
Xpath doesn't work properly sometimes in chrome browser. Use firefox driver to find it, if you are not sure if your xpath is correct, use Firepath. Or in Chrome Xpath Helper.
Your xpath refers to a image, not button.
getDriver().findElement(By.xpath("//img [#ng-click='addAnswer(question)']")).click();
Change this to
getDriver().findElement(By.xpath("//button[#ng-click='addAnswer(question)']")).click();
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!
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
After these manipulations driver shoulda handle element properly.
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']"));
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!