I am trying to get the text content of the items inside the following 2 div elements but getting an error that the element cannot be found. For example the text content for first div is "Text1". How can I get that text?
So far, I have tried:
driver.findElement(By.xpath("//*[#id='ctrlNotesWindow']/div[3]/ul/div[1]/div[2]/div[2]")).getText())
and that complains of not finding that element.
Here is the html code:
<div class="notesData">
<div class="notesDate" data-bind="text: $.format('{0} - {1}', moment(Date).format('MMM DD, YYYY h:mm a'), User)">Text1</div>
<div class="notesText" data-bind="text: Note">Text2 on next line</div>
</div>
Here is the error, I get:
Exception in thread "main" org.openqa.selenium.NoSuchElementException:
Unable to locate element:
{"method":"xpath","selector":"//*[#id='ctrlNotesWindow']/div[3]/ul/div[1]/div[2]/div[2]"}
Don't use meaningless XPath. Your error message tells you "NoSuchElement", so you had your locator wrong, which has nothing to do with getText (not yet). Try using CssSelector or meaningful XPath:
driver.findElement(By.cssSelector("#ctrlNotesWindow .notesData > .notesDate")).getText();
If for whatever reason you'd want to stick with Xpath:
driver.findElement(By.xpath("//div[#class='notesData']/div[#class='notesDate']")).getText();
Otherwise, the css selector answer above is a good option.
Another alternative is to locate the element by class name:
driver.findElement(By.className("notesDate")).getText();
It works with:
driver.findElement(By.xpath("//div[#class='ui-pnotify-text']")).getText();
which is an PNotify notification.
Related
Inspect element from Google Chrome:
<div class="form-style-abc">
<fieldset>
<legend><span class="number">*</span>Search Zone</legend>
Need to retrieve the "Search Zone", however im unable to perform the search and getText()
I had performed the following on Eclipse but getting error:
Code:
String HeaderTxt = driver.findElement(By.xpath("//span[#class = 'number']")).getText();
System.out.println(HeaderTxt);
Error Message:
org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//span[#class = 'number']"}
As per the HTML you have provided to extract the text Search Zone you can use the following line of code :
String HeaderTxt = driver.findElement(By.xpath("//div[#class='form-style-abc']/fieldset/legend[(self::legend) and not (#class='number')]")).getText();
System.out.println(HeaderTxt);
Update 1
As per your comment update as you are still seeing NoSuchElementException possibly you need to wait for the element inducing WebDriverWait as follows :
WebElement HeaderTxtElem = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[#class='form-style-abc']/fieldset/legend[(self::legend) and not (#class='number')]")));
System.out.println(HeaderTxtElem.getText());
Update 2
As you mentioned the element is within an <iframe> tag you need to switch to the exact frame and then look out for the WebElement. Here you can find a detailed discussion on How to switch to frame in Selenium Webdriver Java
Update 3
Xpath Explanation
Through the xpath as "//div[#class='form-style-abc']/fieldset/legend" we have reached till the <legend> node. Now you want to get the text Search Zone only. So being within the <legend> tag you have to trim the child <span> tag which have a class as number. So we added a clause within our xpath not to consider (#class='number') while retrieving the text as in :
String HeaderTxt = driver.findElement(By.xpath("//div[#class='form-style-abc']/fieldset/legend[(self::legend) and not (#class='number')]")).getText();
I am having sticky nav bar which has few list elements where each contains href element. When I tried to locate element I am getting the error.
Following is my HTML code :
<div class="nav" id="sticky">
<div class="container">
<ul class="main-nav">
<li>Dashboard</li>
<li><a href="../MEFAcademicDash/StudentUI/StudentHome.aspx"
title="Academic Dashboard">Academic</a></li>
<li>Notices</li>
I want to locate Academic through Webdriver, I am getting the error like this Unable to locate the element.
org.openqa.selenium.NoSuchElementException: Unable to locate element: /html/body/div/form/div[5]/div[1]/ul/li[2]/a
As per the HTML you have shared you need to induce WebDriverWait for the element to be visible / clickable. To click() on the link with text Academic you can use the following line of code :
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[#class='nav' and #id='sticky']/div[#class='container']/ul[#class='main-nav']//li/a[#title='Academic Dashboard' and contains(#href,'../MEFAcademicDash/StudentUI/StudentHome.aspx')]"))).click();
Can you try below-mentioned xpath?
//*[text() = 'Academic']
Also, use actions driver to perform click action
How to locate an element ::before for click.
Below is the HTML:
<i class="icon-add-circle" tabindex="0" >
::before
</i>
Dear friend you can try below snippet .
driver.findElement(By.className("icon-add-circle"));
To locate the element you can use the following line of code :
driver.findElement(By.xpath("//i[#class='icon-add-circle']"));
driver.findElement(By.xpath("//i[contains(#class,'icon-add-circle')]"));
Above code of line should do. Also try to locate from the parent element always and drill down to the child element.
You can try the following
Get all elements of i tag
1) driver.findElement(By.xpath("//i[#class='icon-add-circle']/*"));
Get text of all elements under i tag
2) driver.findElement(By.xpath("//i[#class='icon-add-circle']//text()"));
Get single element. Specify correct array index.
3) driver.findElement(By.xpath("//i[#class='icon-add-circle']/*[1]"));
I like to select this tag in the page using selenium in java
<input class="btn btn-success addReportBtn" type="submit" />
here is what I've tried so far:
driver.findElement(By.xpath("//input[type=submit]"));
driver.findElement(By.cssSelector("//input[#type='submit']"));
I get these exception for both of them respectively:
org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"input[type=submit]"}
org.openqa.selenium.InvalidSelectorException: invalid selector: An invalid or illegal selector was specified
if I use Jsoup I can easily get it by doing:
System.out.println(document.select("input[type=submit]"));
what am I doing wrong?
As pointed out by #Jason, you are seriously mixing up XPath expression and CSS selector syntaxes. Correct expressions would be:
driver.findElement(By.xpath("//input[#type='submit']"));
driver.findElement(By.cssSelector("input[type=submit]"));
Note that I would not only check the button type since usually there are multiple submit buttons on a page. There is that addReportBtn class I would rely the locator on:
driver.findElement(By.cssSelector("input.addReportBtn"));
And that also gives a plus one to readability.
Try these:
driver.findElement(By.xpath("//input[#type='submit']"));
driver.findElement(By.cssSelector("input[type='submit']"));
I am trying to find an element on a website using Selenium. The page I am looking at is:
http://www.usaswimming.org/DesktopDefault.aspx?TabId=1470&Alias=Rainbow&Lang=en-US
Specifically I am trying to find the element for the "Last Name" input box and send keys in Java. the html looks like this for the text box:
<div class="field">
<input id="ctl62_txtSearchLastName" type="text" maxlength="36" name="ctl00$ctl62$txtSearchLastName">
</div>
Therefore, I initially attempted to get the element through the id which is unique:
WebDriver driver = new InternetExplorerDriver();
driver.get(timeSearchSite);
...
driver.findElement(By.id("ctl62_txtSearchLastName")).sendKeys(lastName);
However this generated the following error:
Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to find element with id == ctl62_txtSearchLastName (WARNING: The server did not provide any stacktrace information)
I also tried to use the name which failed similarly. Since this is a javascipt page I thought that I might need to wait before trying to access the element. I tried an explicit and an implicit wait and both resulted in timeouts. My next thought was that I was in the wrong frame but I cannot seem to find another frame. I tried indexing since I don't have names but I couldn't locate any other frames. There is this at the top of the page which might be messing selenium up:
<noscript><iframe src="//www.googletagmanager.com/ns.html?id=GTM-WMWM93" height="0" width="0"
style="display: none; visibility: hidden"></iframe></noscript>
What could be causing this error? How can I locate the element? Any help would be greatly appreciated/
Using an explicit wait worked for me (tried both chrome and firefox):
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(
ExpectedConditions.visibilityOfElementLocated(By.id("ctl62_txtSearchLastName"))
);
element.sendKeys("test");
Resulted into: