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.
Related
I have html code:
<div class="formCaptionContainer fill-width" data-dyn-bind="sizing: { width: $dyn.layout.Size.available }">
<h1 class="formCaption" data-dyn-bind="text: $data.Caption, click: $data.GoGridView">Expense report for Aditi Mehta - GS1-000282, testing2</h1>
<h2 class="formCaption-context" data-dyn-bind="text: $data.ParentTitleFields">Aditi Mehta : GS1-000282</h2>
</div>
I want to get the value Expense report for Aditi Mehta - GS1-000282, testing2 from /h1 tag
Any one know how to do it?
I've tried :
By.xpath(".//div[#class='formCaption']/h1";
Above showing no element found
By.className("formCaption");
Above showing blank data
By.xpath(".//*[#class='formCaption']/h1");
Above showing no element found
This code work for me very well
String textprint=driver.findElement(By.xpath("//h2[#class='formCaption-context']")).getText();
System.out.println(textprint);
Hope It will solve your problem
Try this one
String msg= driver.findElement(By.xpath(//div[contains(#class='formCaptionContainer')]/h1 ).getText();
Can you try the below Xpath.
String msg=driver.findElement(By.xpath("//div[#class='formCaptionContainer fill-width']/h1[#class='formCaption-context']")).getText();
As per the HTML you have shared, the node attributes looks dynamic to me. So we have to induce WebDriverWait as follows :
new WebDriverWait(driver, 10).until(ExpectedConditions.textToBePresentInElementLocated(By.xpath("//h1[#class='formCaption']"), "Aditi Mehta - GS1-"));
System.out.println(driver.findElement(By.xpath("//h1[#class='formCaption']")).getAttribute("innerHTML"));
You need to consider your locator strategy, the following provide good preferences for general case approach.
location by ID globally unique elements
locate by name for page unique elements
locate by css as catch all case
You should also take a look at the PageObject pattern.
It appears the page is using a JS library to display the contents through late binding/resolution. Your test needs allow for this and wait until the page elements you are interested in are fully rendered. Find out from the developers what the element looks like before it is resolved and afterwards. Late resolution is one considerations in determining your locator strategy. The strategy should include fluent waiting and default time-outs for elements to appear.
This is not a problem of fixing a single locator.
Thanks all for your help. It is working fine now. I use the below code
By headingExpenseText = By.xpath("//*[#class='formCaption'][contains(text(),'Expense report for')]");
public String getTitleEx()
{
return driver.findElement(headingExpenseText).getAttribute("innerHTML");
}
This worked for me:
String titleElem = driver.findElement(By.xpath("//*[#class='formCaptionContainer fill-width']/h1")).getAttribute("innerHTML");
assertEquals("Worked", titleElem);
What do you need exactly, Data displayed on web page or textValue used in HTML code?
If your locator is correct , then instead of getText(), you can try getAttibute("innerHTML") like below,
By.xpath("//*[#class='formCaption'][contains(text(),'Aditi Mehta')]").getAttribute("innerHTML");
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')]
I'm currently having troubles on locating this element with dynamic id. Here are the screenshots below.
What i have right now is the element of Variables (8) //a[contains(.,'Variables (8)')]. What i need is only the "Variables" since the number 8 is always changing.
Any thoughts on this? Any ideas will be much appreciated. Thanks
First of all 'Variables (8)' is not Id, its text. Id's are not dynamic since they represent unique identifier for the web element. This will look like this (based on your example):
<div class="field" id="fieldId">
As for your question, you can find the element by partial linked text:
driver.findElement(By.partialLinkText("Variables"));
This will give you the a element no meter what the number is.
You can try the below:
driver.findElement(By.cssSelector("a:contains('Variables')"));
If you want the word "Variables" , use the below:
String str = driver.findElement(By.cssSelector("a:contains('Variables')")).getText().split(" ")[0];
Hope this Helps....
What I understand from your question is you want to locate the <a> tag which contains text "Variables".
Try using this xpath:
//div[#class="field"]/a[contains(.,"Variables")]
This xpath will locate the <a> tag after the div tag with class name =field and Contains method with <a> tag will find the element which contains text "Variables"
try this:
String varText = driver.findElement(By.cssSelector("div.triggerFirst>div:nth-child(1)>a")).getText();
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
I'm having trouble reading a link inside a div.
Ok, here's what the div looks like:
<div id="AjaxStream" style="clear: both">
<a target="_blank" href="http://www.something.com/">
<img height="370" width="752" border="4" usemap="#Link" src="somefile.png">
</a>
</div>
The following code, to find the div works perfectly fine.
(I tried element.getAttribute("id") - which returned "AjaxStream")
WebElement element = river.findElement(By.xpath("//html/body/div/div[2]/div/div[11]"));
And here is what's not working:
WebElement element = driver.findElement(By.xpath("//html/body/div/div[2]/div/div[11]/a"));
This should actually fine the link-element, but it doesn't. Any ideas?
Thanks in advance.
##Edit:
Nevermind - I fixed it. The problem was that the element wasn't loaded. I added a Thread.sleep(1000) before trying to find the element - and now it works perfectly fine.
try
WebElement element = driver.findElement(By.xpath("//div[#id='AjaxStream']/a"));
String link = element.getAttribute("href");
//I need 6 characters but it's a 1 "char" fix
Have a look at your xpath...to me that is unreadable. If someone comes in later in a few months, are they able to translate that xpath in to tag you're looking for? A better solution would be a to add an id attribute to the tag you are interested, and find it by that ID.