I need to capture the error messages displayed, i tried many methods but every method throws exception --unable to find element,
pls help with the code. These are the methods i tried.Also, there is no ID, it is div element. something like this...
<div id="webformErrors" class="text" name="errorContent">
<div>
There were 4 errors:
<ul>
<li>
You did not enter a value for:
<b>First Name</b>
</li>
<li>
You did not enter a value for:
<b>Last Name</b>
</li>
<li>
<li>
//String errormsg;
![enter image description here][1]errormsg = Hcd.findElement(By.xpath("//div[#id=webformErrors']/text()")).getText();
// WebElement divElement = Hcd.findElement(By.className("errorContent"));
// Hcd.findElement(By.name("There were 4 errors:")).isDisplayed();
**String pstring = Hcd.findElement(By.id("webformErrors")).getText();
System.out.println(pstring);
You have given the classname and name wrong. classname is "text" and name is "errorContent".
WebElement divElement = Hcd.findElement(By.className("text"));
Related
I'm trying to ignore an item and not parse it on Jsoup
But css selector "not", not working !!
I don't understand what is wrong ??
my code:
MangaList list = new MangaList();
Document document = getPage("https://3asq.org/");
MangaInfo manga;
for (Element o : document.select("div.page-item-detail:not(.item-thumb#manga-item-5520)")) {
manga = new MangaInfo();
manga.name = o.select("h3").first().select("a").last().text();
manga.path = o.select("a").first().attr("href");
try {
manga.preview = o.select("img").first().attr("src");
} catch (Exception e) {
manga.preview = "";
}
list.add(manga);
}
return list;
html code:
<div class="col-12 col-md-6 badge-pos-1">
<div class="page-item-detail manga">
<div id="manga-item-5520" class="item-thumb hover-details c-image-hover" data-post-id="5520">
<a href="https://3asq.org/manga/gosu/" title="Gosu">
<img width="110" height="150" src="https://3asq.org/wp-content/uploads/2020/03/IMG_4497-110x150.jpg" srcset="https://3asq.org/wp-content/uploads/2020/03/IMG_4497-110x150.jpg 110w, https://3asq.org/wp-content/uploads/2020/03/IMG_4497-175x238.jpg 175w" sizes="(max-width: 110px) 100vw, 110px" class="img-responsive" style="" alt="IMG_4497"/> </a>
</div>
<div class="item-summary">
<div class="post-title font-title">
<h3 class="h5">
<span class="manga-title-badges custom noal-manga">Noal-Manga</span> Gosu
</h3>
If I debug your code and extract the HTML for:
System.out.println(document.select("div.page-item-detail").get(0)) (hint use the expression evaluator in IntelliJ IDEA (Alt+F8 - for in-session, real-time debugging)
I get:
<div class="page-item-detail manga">
<div id="manga-item-2003" class="item-thumb hover-details c-image-hover" data-post-id="2003">
<a href="http...
...
</div>
</div>
</div>
It looks like you want to extract the next div tag down with class containing item-thumb ... but only if the id isn't manga-item-5520.
So here's what I did to remove that one item
document.select("div.page-item-detail div[class*=item-thumb][id!=manga-item-5520]")
Result size: 19
With the element included:
document.select("div.page-item-detail div[class*=item-thumb]")
Result size: 20
You can also try the following if you want to remain based at the outer div tag rather than the inner div tag.
document.select("div.page-item-detail:has(div[class*=item-thumb][id!=manga-item-5520])")
I want to retrieve visitor ID from “visitor” or "visitor.VisitorId" . but below code I use to retrieve data but successfully run without any error but I received value is null.
HTML Code:-
<ul class="sidebar-menu">
<li id="visitorView" class="treeview active">
<a>
<ul id="visitorViewMenu" class="treeview-menu menu-open" style="display: block;">
<!-- ngRepeat: visitor in Visitors -->
<li class="ng-scope" ng-repeat="visitor in Visitors" style="">
<a id="visitor.VisitorId" class="ng-binding" ng-click="select(visitor)">
<countryflag class="flagimg ng-isolate-scope" visitor="visitor">
<span class="chattabname"/>
A
<span class="timmer1 pull-right" runtimer="{"VisitorID":"c2c45b4d-5077-492f-afd6-88ab3bba99cd","Name":"A","StartTime":"2016-09-09 10:33:21","WidgetId":"7fcf22c6-4a9d-4701-9865-b8a85d597862","ConnectionId":"edc7d72b-8217-4961-81ff-f4ef4138bc3b","TimeZone":"Asia/Colombo","CountryCode":"lk","VisitorName":null,"Department":null,"CompanyId":"a4afbd8b-1de9-49d9-8fe6-4ec8119f4bb8"}">
</a>
</li>
<!-- end ngRepeat: visitor in Visitors -->
<li>
</ul>
</li>
<li class="treeview">
<li class="treeview">
</ul>
Selenium Code:-
**1st method :-**
WebElement cityField = driver.findElement(By.cssSelector("a[ng-click='select(visitor)']"));
**2nd method :-**
WebElement cityField = driver.findElement(By.cssSelector("a[id='visitor.VisitorId']"));
**Output**
System.out.println("+++-- "+cityField.getAttribute("value"));
Try using getText() which will return innerText of the <a> element as below :-
WebElement cityField = driver.findElement(By.id("visitor.VisitorId"));
System.out.println("+++-- " + cityField.getText());
Or if you want to get span element where visitorId present in runtimer attribute value, you should locate span element and get runtimer attribute value as :-
WebElement cityField = driver.findElement(By.cssSelector("a[id = 'visitor.VisitorId'] span.timmer1"));
String runtimeData = cityField.getAttribute("runtimer");
//Now do some programming stuff to retrieve visitor id
runtimer attribute data looks like in json format, so you can retrieve any data after converting in into org.json.JSONObject by passing their key as below :-
import org.json.JSONException;
import org.json.JSONObject;
public static Object getValue(String data, String key) throws JSONException {
JSONObject jObject = new JSONObject(data);
return jObject.get(key);
}
String visitorID = (String) getValue(runtimeData, "VisitorID");
System.out.println(visitorID);
Output :-
c2c45b4d-5077-492f-afd6-88ab3bba99cd
As OP suggested, we can use split() function as well to retrieve data as :-
String[] splitS = runtimeData.split(",");
for(int i =0; i < splitS.length; i++)
{
System.out.println("splitS" + splitS[i]);
}
If I understand correctly the value you are looking for is in the runtimer attribute that located in descendant element of id="visitor.VisitorId", you need to put that in getAttribute() method
WebElement cityField = driver.findElement(By.cssSelector("#visitor.VisitorId > .timmer1"));
String attributeData = cityField.getAttribute("runtimer");
String visitorId = attributeData.split(",");
System.out.println("+++-- " + visitorId);
Output: +++-- "VisitorID":"c2c45b4d-5077-492f-afd6-88ab3bba99cd"
I am parsing the following html by Selenium FirefoxDriver -
<div id="primaryNav" style='background: url("https://images-na.ssl-images-amazon.com/images/G/31/associates/network/08-ui-elements/primaryNavBackground._V161555288_.gif") repeat-x bottom;'>
<div id="menuh">
<ul>
<li style="visibility:hidden;
height:24px"/>
<li style='background:url("https://images-na.ssl-images-amazon.com/images/G/31/associates/network/08-ui-elements/tab-unslected-right._V161557413_.gif") no-repeat right top;'>
▼</span>
<div class="parent">
<div class="dropdownlinks">
<div class="subitem">Product Links</div><div class="subitem">Banner Links</div><div class="subitem">Link to Any Page</div><div class="subitem">Link Checker</div>
</div>
</div>
</li>
<li style='background:url("https://images-na.ssl-images-amazon.com/images/G/31/associates/network/08-ui-elements/tab-unslected-right._V161557413_.gif") no-repeat right top;'>
<a href="http://widgets.amazon.in/?_encoding=UTF8&store=httpswwwvanta-21&tag=httpswwwvanta-21" style='float:left;background:url("https://images-na.ssl-images-amazon.com/images/G/31/associates/network/08-ui-elements/tab-unslected-left._V161554471_.gif") no-repeat left top;'>Widgets</a>
</li>
<li style='background:url("https://images-na.ssl-images-amazon.com/images/G/31/associates/network/08-ui-elements/tab-unslected-right._V161557413_.gif") no-repeat right top;'>
<a href="/gp/advertising/api/detail/main.html" style='float:left;background:url("https://images-na.ssl-images-amazon.com/images/G/31/associates/network/08-ui-elements/tab-unslected-left._V161554471_.gif") no-repeat left top;'>Product Advertising API</a>
</li>
<li style='background:url("https://images-na.ssl-images-amazon.com/images/G/31/associates/network/08-ui-elements/tab-unslected-right._V161557413_.gif") no-repeat right top;'>
▼</span>
<div class="parent"><div class="dropdownlinks">
<div class="subitem"><a href="/gp/associates/network/reports/report.html?ie=UTF8&reportType=earningsReport" >Earnings Report</a>
</div><div class="subitem"><a href="/gp/associates/network/reports/report.html?ie=UTF8&reportType=ordersReport" >Orders Report</a>
</div><div class="subitem"><a href="/gp/associates/network/reports/report.html?ie=UTF8&reportType=linkTypeReport" >Link-Type Report</a>
</div><div class="subitem"><a href="/gp/associates/network/reports/report.html?ie=UTF8&reportType=trendsReport" >Daily Trends</a>
</div><div class="subitem"><a href="/gp/associates/network/reports/report.html?ie=UTF8&reportType=tagsReport" >Tracking ID Summary Report</a>
</div>
</div>
</div>
</li>
</ul>
</div>
</div>
I am trying to select the "Earnings Report" from the dropdown menu.
I tried like this --
dropDownButton: WebElement = driver.findElement(By.xpath(".//a[#href='https://affiliate-program.amazon.in/gp/associates/network/reports/report.html?ie=UTF8&reportType=earningsReport']"))
dropDownButton.click()
I also tried like this --
val dropDownButton = driver.findElement(By.linkText("Earnings Report"))
dropDownButton.click()
In both the cases, The code runs only when I hover my mouse over the dropdown menu. No manual click is required.
I also tried the following code which I am not sure if correct -
import scala.collection.JavaConversions._
def selectValueFromDropdown( value: String) = {
var options = driver.findElements(By.id("menuh"));
for(option <- options) {
if (value.equals(option.getText())) {
option.click()
}
}
}
selectValueFromDropdown("Earnings Report")
I am kinda lost here. Please suggest a solution in either Java or Scala.
EDIT: I get to this page after log-in from the main page. Can that be a problem?
Please try this:
First select the drop down and then select by value or Index-
Select drpdown = new Select(driver.findElement(By.xpath("Locator of the dropdown"));
drpdown.SelectByValue("Earning Report");
If "Earning Report" is a visible Text then-
drpdown.selectByVisibleText("Earning Report");
As you mentioned that you have to hover your mouse over the dropdown menu for it to work. Your menu also has sub-menus. So, before clicking the link you need to use the "perform" method of "Actions". In this manner it allows Selenium to spot a particular sub-menu while holding the menu. The code for that is:
val menuElement = driver.findElement(By.id("menuh"))
/* If the css selector used below does not match the element that
* fires the hover action then check which element fires it and update
* the selector */
val subMenuElement = driver.findElement(By.cssSelector("#menuh li:nth-child(5)"))
val earningsReportElement = driver.findElement(By.linkText("Earnings Report"))
val action = new Actions(driver)
action.moveToElement(menuElement).perform()
action.moveToElement(subMenuElement).perform()
action.moveToElement(earningsReportElement)
action.click()
action.perform()
try this-
driver.findElement(
By.xpath("//a[contains(#href,'earningsReport')]"))
.click();
The solution given by #Dagojvg worked after re-arranging the expressions.
import org.openqa.selenium.interactions.Actions
val action = new Actions(driver)
val menuElement = driver.findElement(By.id("menuh"))
action.moveToElement(menuElement).perform()
val subMenuElement = driver.findElement(By.cssSelector("#menuh li:nth-child(5)"))
action.moveToElement(subMenuElement).perform()
val earningsReportElement = driver.findElement(By.linkText("Earnings Report"))
action.moveToElement(earningsReportElement)
action.click()
action.perform()
I have been trying to get the anchor link via WebDriver but somehow, things aren't working as desired and I am not getting the element.
Below is the HTML Structure:
<div id="1">
<table width="100%">
<tr>
<td>.....</td>
<td>
<ul class="bullet">
<li>....</li>
<li>....</li>
<li>
myText
</li> // myText is the text I am searching for
</ul>
</td>
</tr>
</table>
<div>....</div>
</div>
The <li> elements contains anchor tags with links only. No id or any other attribute they contain. The only difference is the text displayed by them and hence, I am passing myText to detect exactly what I need.
And for this, the java code I have been trying is:
driver.get("url");
Thread.sleep() //waiting for elements to get loaded. Exceptional Handling not done.
WebElement divOne = driver.findElement(By.id("1"));
WebElement ul = divOne.findElement(By.className("bullet"));
WebElement anchor = null;
try{
anchor = ul.findElement(By.partialLinkText("myText"));
}catch(NoSuchElementException ex){
LOGGER.warn("Not able to locate tag:" + linkText + "\n");
}
String myLink = anchor.getAttribute("href"); // null pointer exception
I don't understand why is this happening. What is the correct way to do this? Should I use some other method?
driver.findElement(By.xpath("//a[contains(text(),'myTest')]"));
that searches for myTest text. I haven't tried the code, I hope it helps you
you can use any of the below. Should work for you
List<WebElement> anchor = driver.findElements(By.partialLinkText("myText"));
or
driver.findElements(By.linkText("myText"))
or
driver.findElements(By.Xpath("//a[contains(text(),'myText')]"));
String myLink = anchor.getAttribute("href"); // null pointer exception
You are getting exception because you didn't set the anchor value, anchor variable is created and intialized to null and after that it is never updated.
I think the correct code should be below
String myLink = element.getAttribute("href");
Try using a WebDriver instance directly instead of WebElement instance...i.e., use driver instead of ul..
you can try with wait,
element = new WebDriverWait(driver,10).until(ExpectedConditions.visibilityOfElementLocated(By.linkText("myText")));
How do I get a value from href?
like this eg:
<div id="cont"><div class="bclass1" id="idOne">Test</div>
<div id="testId"><a href="**NEED THIS VALUE AS STRING**">
<img src="img1.png" class="clasOne" />
</a>
</div>
</div>
</div>
I need that value as string.
I've tried with this:
String e = driverCE.findElement(By.xpath("//div[#id='testId']")).getAttribute("href");
JOptionPane.showMessageDialog(null, e);
But just returns NULL value...
You have pointed your element to 'div' instead of 'a'
Try the below code
driverCE.findElement(By.xpath("//div[#id='testId']/a")).getAttribute("href");
If you got more than one anchor tag, the following code snippet will help to find all the links pointed by href
//find all anchor tags in the page
List<WebElement> refList = driver.findElements(By.tagName("a"));
//iterate over web elements and use the getAttribute method to
//find the hypertext reference's value.
for(WebElement we : refList) {
System.out.println(we.getAttribute("href"));
}