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
Related
I am trying to access a web element in this example I am trying to access the className
driver.findElement(By.className("more-option")).click();
from bellow but it fails.
<div class="text-center">
<a class="small text-muted js-more-options" href="#">More
Options</a> = $0
</div>
My goal is to be able to test the ability to click More options button
Edit
I have tried
driver.findElement(By.cssSelector("td[title='More options']")).click();
and
driver.findElement(By.partialLinkText("options")).click();
There are to many option you can click on element.You can use contains().
Contains() is a method used in XPath expression.
driver.findElement(By.XPath("//a[contains(text(),'More Options')]")).Click();
or
driver.findElement(By.XPath("//a[contains(#class,'small')]")).Click();
if you get more than one element then you have to use index and click on particular element.
Find element using By.className just for single class name.
Try following approach.
By css selector:
driver.find_element_by_css_selector('div.text-center a.small.text-muted.js-more-options').click()
driver.find_element_by_css_selector('a[class="small text-muted js-more-options"]').click()
By xpath:
driver.find_element_by_xpath('//div[#class="text-center"]//a[#class="small text-muted js-more-options"]').click()
By partial link text:
driver.find_element_by_partial_link_text('Options').click()
I am trying to the following element:
<span data-dojo-attach-point="lN" role="btn" aria-selected="false" class="xTreeNLbl">Find</span>
The following is the Java code:
private WebElement search_btn = driver.findElement(By.xpath("//div[#data-dojo-attach-point='lN' and contains(text(),'Find')"));
search_btn.click();
It's unable to find the element. Please help. Thanks.
you are wrong at the xpath //div
as this is <span> tag not <div> tag
you should write like this,
driver.findElement(By.xpath("*//span[text()='Find']")).click();
Your xpath is almost correct, you just need to correct tag name with Span
driver.findElement(By.xpath("//span[#data-dojo-attach-point='lN' and contains(text(),'Find')"));
One option may be to try and find the element using one of it's attribute, like its class if it's unique.
Taken from this previous question (Find element by attribute), the formula is:
element[attribute='attribute-value']
So if you have,
You can find it using:
By.cssSelector("a[href='mysite.com']");
Please try with the follow code:
//span[#role='btn' and #class='xTreeNLbl']
If this code does not solve your error, pass me the html dom code of the page and I will help.
Trying to find an xpath expression to use in:
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("XPATH HERE"))).click();
The element says "Invite Users" on the page and i need to be able to click on it
I need to find the element /a[#id='inviteUsers_8ef17ba4-b739-4fb6-8198-3862ea84c381_toggle'] but the problem is the characters after "inviteUsers_" is dynamically generated
I have already tried these:
"//*[contains(.,'Invite Users')]";
"//a[contains(.,'Invite Users')]";
And these give NoSuchElement exceptions.
This is the complete XPATH:
/html/body/div[#class='col-xs-10 col-xs-offset-2 main']/fieldset[#class='form-horizontal']/div[#id='roles']/div[#id='entitlements']/div[#class='panel panel-default '][3]/div[#id='service_8ef17ba4-b739-4fb6-8198-3862ea84c381']/div[#class='panel-body']/div[#class='panel panel-default'][1]/div[#class='panel-heading']/h4[#class='panel-title']/a[#id='inviteUsers_8ef17ba4-b739-4fb6-8198-3862ea84c381_toggle']
You can solve it with starts-with():
//a[starts-with(#id, "inviteUsers_")]
If this does not work try find a unique parent.
//div[contains(#id, 'service')]//a[contains(#id, 'inviteUsers')]
The following code says that "The method text() is undefined for the type Test" and prompts me to create a new function text() in class Test.
driver.findElement(By.xpath(contains(text(), "menu")));
I am using Eclipse Kepler and Selenium 2.39.0.
The exception i receive is : org.openqa.selenium.NoSuchElementException.
I am not able to figure out where am i going wrong.
XPath expressions need to be surrounded by quotes - and since the expression you are trying to parse also contains a string literal, I would suggest you switch the literal to single apostrophe '. Also, unless you expect the root element to contain the text menu, you'll need to be more specific about the element you are searching for. For example, the following xpath:
driver.findElement(By.xpath("//*[contains(text(), 'menu')]"));
Will find the li element with the text "menu" (note xpath is case-sensitive):
<html>
<foo>
<bar>
<ul>
<li id="123">menu</li>
</ul>
</bar>
</foo>
</html>
If possible, be even more certain, e.g. if you know that it is a li element:
driver.findElement(By.xpath("//li[contains(text(), 'menu')]"));
Edit (OP put the actual html up)
This will find the DIV element:
driver.findElement(By.xpath("//DIV[#style[contains(., 'menubar_menubutton.png')]]"));
Note that xpath is case sensitive - so you'll need to duplicate the SHOUTCASE tags.
i feel there is a much easier way to do this.
i'd personally just do:
WebElement we = driver.findElement(By.Id("123"))
or if you'd like to leverage css selectors you could do:
WebElement we = driver.findElement(By.cssSelector("li:nth-child(1)")) //baring you know the index of the list
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.