Using Jsoup to find an element with particular text - java

I want to select an element with specific text from the HTML using JSoup. The HTML is
<div class="logonContainer" style="padding-left: 300px; background-color: #3d5fa3; padding-top: 110px">
<div id="bglogodiv" style="background-image: url(15.0.847/themes/resources/ctslogo.jpg); border-radius: 25px; width: 800px">
<br />
<br />
<div id="lgnDiv" class="logonDiv" onkeypress="return checkSubmit(event)">
<div class="signInImageHeader" role="heading" aria-label="Outlook Web App ">
<img class="mouseHeader" src="/owa/auth/15.0.1263/themes/resources/owa_text_blue.png" alt="Outlook Web App " />
</div>
<div>
<div id="right" style="width: auto; float: right">
<div class="signInInputLabel" id="userNameLabel" aria-hidden="true">
User name:
</div>
<div>
<input id="username" name="username" class="signInInputText" role="textbox" aria-labelledby="userNameLabel" />
</div>
<div class="signInInputLabel" id="passwordLabel" aria-hidden="true">
Password:
</div>
<div>
<input id="password" onfocus="g_fFcs=0" name="password" value="" type="password" class="signInInputText" aria-labelledby="passwordLabel" />
</div>
<div>
<input id="passwordText" onfocus="g_fFcs=0" name="passwordText" value="" style="display: none;" class="signInInputText" aria-labelledby="passwordLabel" />
</div>
<div class="showPasswordCheck signInCheckBoxText">
<input type="checkbox" id="showPasswordCheck" class="chk" onclick="showPasswordClick()" />
<span>Show password</span>
</div>
</div>
I'm able to find the element using input id for username and password and pass my static credentials in to it using the below code. I want my code to find these elements dynamically during the runtime using some keywords and pass the static credentials. How would i do that. I may not know the element id, text or xpath during run time it can be anything. Kindly suggest how this can be done or help me with a working example.
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.junit.Assert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.w3c.dom.Document;
public class Dynamic_LoginPass {
public static final String Username = "admin";
public static final String Password = "admin";
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
System.setProperty("webdriver.chrome.driver","D:\\Eclipse\\workspace\\PopUp_Test\\lib\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://www.nature.com/");
try {
TimeUnit.SECONDS.sleep(5);
} catch (InterruptedException e) {
e.printStackTrace();
}
String windowHandle = driver.getWindowHandle();
Assert.assertNotNull(windowHandle);
Thread.sleep(10000);
//driver.findElement(By.xpath("//*[#id=\"easycont\"]/div/div[2]/div[2]/div[2]/button")).click();
String html_content1 = driver.getPageSource();
// Jsoup makes DOM here by parsing HTML content
org.jsoup.nodes.Document doc1 = Jsoup.parse(html_content1);
System.out.println("Result:"+doc1);
int test= doc1.select("div:contains(signInInputLabel)").size();
System.out.println("testtesttesttest::"+test);
if(driver.findElement(By.cssSelector("input[id=username]")) != null){
driver.findElement(By.cssSelector("input[id=username]")).sendKeys(Username);
driver.findElement(By.cssSelector("input[id=password]")).sendKeys(Password);
driver.findElement(By.xpath("//*[#id=\"bglogodiv\"]/div[7]/div/span")).click();
System.out.println("#############clicked################");
} else if(driver.findElement(By.cssSelector("input[id=password]")) != null){
System.out.println("clicked in else if");
}
}
}
Thanks in advance

In my opinion the best way to use Jsoup FormElement class.
You can check how this should be done.
In this example
This practical approach use only jsoup. Therefore no need for WebDriver.

Related

select text from span in selenium java enviroment , when its not a drop box

so I want to select the text of span in selenium java, as it is not a normal drop-down box, I would have done it easily, I am using XPath I am able to open that drop-down box (where we select different sizes of shirt) type but no able to select text from it, I am sharing website link and also sharing my code
https://www.ounass.ae/shop-emporio-armani-camouflage-print-t-shirt-in-mercerised-cotton-for-men-215380566_20.html
my Code,
package First_Test;
import java.time.Duration;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.WindowType;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ISelect;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.Keys;
import io.github.bonigarcia.wdm.WebDriverManager;
public class Practice_First {
public static void main(String[] args) throws InterruptedException {
//System.setProperty("webdriver.chrome.driver", "./Driver/chromedriver.exe");
WebDriverManager.chromedriver().setup();
WebDriver driver = new ChromeDriver();
driver.get("https://www.ounass.ae/");
driver.manage().window().maximize();
Thread.sleep(3000);
String originalWindow= driver.getWindowHandle();
Thread.sleep(5000);
driver.findElement(By.id("wzrk-cancel")).click();
// driver.findElement(By.xpath("//button[#id='wzrk-cancel']")).click();
driver.findElement(By.xpath("//a[#href='/men']//div[#class='Gender-centralize']//span[#class='Gender-CTA'][normalize-space()='Shop Now']")).click();
Thread.sleep(2000);
//driver.findElement(By.cssSelector("a[href='shop-emporio-armani-camouflage-print-t-shirt-in-mercerised-cotton-for-men-215380566_20.html']")).click();
// driver.findElement(By.cssSelector("a[title=\"Camouflage Print T-shirt in Mercerised Cotton\"]")).click();
driver.findElement(By.xpath("//span[normalize-space()='675 AED']")).click();
Thread.sleep(3000);
driver.findElement(By.xpath("//button[#title='Blue']")).click();
Thread.sleep(2000);
driver.findElement(By.xpath("//div[#class='Select-control']")).click();
Thread.sleep(1000);
//driver.findElement(By.xpath("//div[#class='Select Dropdown SizeSelection-options
Thread.sleep(1000); */
//driver.close();
}
}
My html code :
<div class="Select Dropdown SizeSelection-options SizeSelection-fixedSize has-value Select--single" xpath="1">
<input name="sizeCode" type="hidden" value="75">
<div class="Select-control">
<div class="Select-multi-value-wrapper" id="react-select-sizeCode--value">
<div class="Select-value">
<span class="Select-value-label" role="option" aria-selected="true" id="react-select-sizeCode--value-item">M</span>
</div>
<div aria-expanded="false" aria-owns="" aria-activedescendant="react-select-sizeCode--value" aria-disabled="false" class="Select-input" role="combobox" style="border:0;width:1px;display:inline-block" tabindex="0">
</div>
</div>
<span class="Select-arrow-zone"><svg width="12" height="7" viewBox="0 0 20 10" class="svg">
<path d="M0 0l10 10L20 0" stroke="#2d2d2d" class="svg-stroke">
</path>
</svg>
</span>
</div>
</div>
Try below xPath for clicking on dropdown Item.
driver.FindElement(By.Xpath("//*[text()='XS']").Click();

JSoup: How to get info which is under some tag?

I have such html page:
<a name="robots"></a>
<div class="dnb">
<div class="info_outer">
<div class="info">
<div class="name">TEXT1 <span class="t">TEXT2</span></div>
<div class="role">SOMEROLE1</div>
</div>
</div>
</div>
<a name="humans"></a>
<div class="dnb">
<div class="info_outer">
<div class="info">
<div class="name">TEXT3 <span class="t">TEXT4</span></div>
<div class="role">SOMEROLE2</div>
</div>
</div>
</div>
<div class="dnb">
<div class="info_outer">
<div class="info">
<div class="name">TEXT5 <span class="t">TEXT6</span></div>
<div class="role">SOMEROLE3</div>
</div>
</div>
</div>
<div class="dnb">
<div class="info_outer">
<div class="info">
<div class="name">TEXT7 <span class="t">TEXT8</span></div>
<div class="role">SOMEROLE4</div>
</div>
</div>
</div>
I need to get info from these divs (names and roles). But only from those, which are under "humans" separator.
Is it possible with JSoup?
Yes, it is possible. Learn selector syntax.
import java.io.File;
import java.io.IOException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
public class JSoupSelectors {
public static void main(String[] args) throws IOException {
File input = new File("WeAreTheRobots.xml");
Document doc = Jsoup.parse(input, null);
for (Element human : doc.select("a[name=humans]")) {
Element info = human.nextElementSibling().selectFirst("div.dnb>div.info_outer>div.info");
String name = info.selectFirst(">div.name>span.t").ownText();
System.out.println("Name = " + name);
String role = info.selectFirst(">div.role").ownText();
System.out.println("Role = " + role);
}
}
}
Output:
Name = TEXT4
Role = SOMEROLE2

Why is the form's onSubmit method not called when a DateTimeField is present?

A question regarding Apache Wicket: I have a form which contains several text input fields. It works fine until I add a DateTextField. The overriden method onSubmit() is simply not called anymore. I looked at the Wicket examples but cannot see a major difference to my code.
Here's the html code:
<html xmlns:wicket="http://wicket.apache.org">
<head>
<title>Students</title>
</head>
<body>
<div class="container">
<form id="createStudent" wicket:id="createStudent">
<div>
<span id="lastNameLabel"><wicket:message key="lastNameLabel" /></span>
<input id="lastName" wicket:id="lastName" type="text" />
</div>
<div>
<span id="firstNameLabel"><wicket:message key="firstNameLabel" /></span>
<input id="firstName" wicket:id="firstName" type="text" />
</div>
<div>
<span id="dateOfBirthLabel"><wicket:message key="dateOfBirthLabel" /></span>
<input id="dateOfBirth" wicket:id="dateOfBirth" type="text" />
</div>
<div>
<input id="submit" type="submit" value="" wicket:message="value:submitLabel"/>
</div>
</form>
</div>
</body>
</html>
The corresponding java file:
package it.foo;
import java.util.Locale;
import org.apache.wicket.Session;
import org.apache.wicket.datetime.StyleDateConverter;
import org.apache.wicket.datetime.markup.html.form.DateTextField;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.joda.time.DateTime;
public class CreateStudentForm extends Form<Student> {
private static final long serialVersionUID = 1L;
private String lastName;
private String firstName;
private DateTime dateOfBirth;
#SpringBean
StudentDao studentDao;
public CreateStudentForm(String id) {
super(id);
setDefaultModel(new CompoundPropertyModel<>(this));
add(new TextField<String>("lastName"));
add(new TextField<String>("firstName"));
final DateTextField dateOfBirthField = new DateTextField("dateOfBirth", new StyleDateConverter("S-", true)) {
private static final long serialVersionUID = 1L;
#Override
public Locale getLocale() {
return Session.get().getLocale();
}
};
dateOfBirthField.setType(DateTime.class);
add(dateOfBirthField);
}
#Override
protected void onSubmit() {
// does not get called as soon as the DateTextField is present. Works fine otherwise.
Student student = new Student(this.lastName, this.firstName, this.dateOfBirth);
studentDao.store(student);
setResponsePage(StudentsPage.class);
}
}
I had a look at the html which is rendered in the browser. If the dateTextField is NOT present, it looks like this:
<html>
<head>
<title>Students</title>
</head>
<body>
<div class="container">
<form id="createStudent" method="post" action="./?2-1.IFormSubmitListener-createStudent"><div style="width:0px;height:0px;position:absolute;left:-100px;top:-100px;overflow:hidden"><input type="hidden" name="createStudent_hf_0" id="createStudent_hf_0" /></div>
<div>
<span id="lastNameLabel">Nachname: </span>
<input id="lastName" type="text" value="" name="lastName"/>
</div>
<div>
<span id="firstNameLabel">Vorname:</span>
<input id="firstName" type="text" value="" name="firstName"/>
</div>
<!-- <div>
<span id="dateOfBirthLabel"><wicket:message key="dateOfBirthLabel" /></span>
<input id="dateOfBirth" wicket:id="dateOfBirth" type="text" />
</div> -->
<div>
<input id="submit" type="submit" value="Schüler anlegen"/>
</div>
</form>
</div>
</body>
</html>
As soon as the dateTextField is present, the form has additional divisions with JavaScript calls.
<html>
<head>
<title>Students</title>
</head>
<body>
<div class="container">
<form id="createStudent" method="post" action="./?6-7.IFormSubmitListener-createStudent"><div style="width:0px;height:0px;position:absolute;left:-100px;top:-100px;overflow:hidden"><input type="hidden" name="createStudent_hf_0" id="createStudent_hf_0" /></div><div style="width:0px;height:0px;position:absolute;left:-100px;top:-100px;overflow:hidden"><input type="text" tabindex="-1" autocomplete="off"/><input type="submit" tabindex="-1" name="p::submit" onclick=" var b=document.getElementById('submit'); if (b!=null&&b.onclick!=null&&typeof(b.onclick) != 'undefined') { var r = Wicket.bind(b.onclick, b)(); if (r != false) b.click(); } else { b.click(); }; return false;" /></div>
<div>
<span id="lastNameLabel">Nachname: </span>
<input id="lastName" type="text" value="Matthias" name="lastName"/>
</div>
<div>
<span id="firstNameLabel">Vorname:</span>
<input id="firstName" type="text" value="Tonhäuser" name="firstName"/>
</div>
<div>
<span id="dateOfBirthLabel">Geburtsdatum: </span>
<input id="dateOfBirth" type="text" value="06.09.17" name="dateOfBirth"/>
</div>
<div>
<input id="submit" type="submit" value="Schüler anlegen" name="p::submit"/>
</div>
</form>
</div>
</body>
</html>
I don't quite understand why the additional divisions are there all off a sudden. My guess is that the JavaScript call does not work.
Thanks.
It is likely that the onSubmit() method does not call may be because of incorrect validation of dateField's value. You have to add feedback panel to you page and check .
You can override the onError(...) method and look whats going on..

Not connecting to database through java

Actually I am trying to insert values from java servlet to postgres DB but they are not inserting.
Thanks in advance.
My html page is
<div class="container4">
<h2>Registration form</h2>
<form action="Register" method="get" >
<div class="form-group">
<label for="firstname">First Name:</label>
<input type="text" class="form-control" id="firstname" placeholder="Enter firstname" name="firstname">
</div><br><br>
<div class="form-group">
<label for="Username">Username:</label>
<input type="text" class="form-control" id="username" placeholder="Enter username" name="username">
</div><br><br>
<div class="form-group">
<label for="email">Email:</label>
<input type="email" class="form-control" id="email" placeholder="Enter email" name="email">
</div><br><br>
<div class="form-group">
<label for="pwd">Password:</label>
<input type="password" class="form-control" id="pwd" placeholder="Enter password" name="pwd">
</div><br><br>
<div class="form-group">
<label for="address">Address:</label>
<input type="text" class="form-control" id="address" placeholder="Enter address" name="address">
</div><br><br>
<button type="submit" class="btn btn-default" id="submit">Submit</button>
</form>
</div>
My dbconnection page is
package dbConnection;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.SQLException;
/**
*
* #author java2
*/
public class Dbconn {
public static Connection getConnection() {
Connection con = null;
try {
Class.forName("org.postgresql.Driver");
con = DriverManager.getConnection("jdbc:postgresql://localhost:5432/classes","postgres", "admin123$");
System.out.println("db successfull");
} catch (Exception ex) {
ex.printStackTrace();
System.out.println("db not successfull");
}
return con;
}
}
Register.java
package register;
import dbConnection.Dbconn;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.Statement;
import java.util.Random;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Register extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
Connection con = null;
Statement st = null;
String n=request.getParameter("firstname");
String p=request.getParameter("username");
String e=request.getParameter("email");
String c=request.getParameter("pwd");
String d=request.getParameter("address");
try {
con = Dbconn.getConnection();
st = con.createStatement();
int i = st.executeUpdate(
"insert into register (firstname, username, email, pwd, address) values('"+n+"','"+p+"','"+e+"','"+c+"','"+d+"')");
con.commit();
System.out.println("hello successfull");
if (i == 0) {
response.sendRedirect("login.html?msg=success");
} else {
response.sendRedirect("register.html?msg=failed");
}
} catch (Exception ex) {
ex.printStackTrace();
}
}}
When I run this program I am getting like
http://localhost:8088/trial/Register?firstname=jack&username=jack&email=jack%40gmail.com&pwd=jack&address=hyd
I am getting all the details in url and displays blank page without errors. I am not understanding where I went wrong.

jsoup post login is not being redicted

I am having issues trying to log using the jsoup to log in. I am not sure if this one has any cookies? Am I missing something? why can't i log in?
import java.io.IOException;
import java.util.Map;
import org.jsoup.Connection.Method;
import org.jsoup.Connection.Response;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
public class LoginJtest {
/**
* #param args
* #throws IOException
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
// Connect to page and parse html into a 'Document'
//This will get you the response.
Response res = Jsoup
.connect("https://pslweb01.ciq.labs.att.com:8080/dis/login.jsp")
.data("txtUserName", "myusername", "txtPassword", "mypassword")
.method(Method.POST)
.execute();
//This will get you cookies
Map<String, String> loginCookies = res.cookies();
//And this is the easiest way I've found to remain in session
Document doc = Jsoup.connect("https://pslweb01.ciq.labs.att.com:8080/dis/")
.cookies(loginCookies)
.get();
String title = doc.title();
System.out.println(title);
}
}
This is the the source of the website for the login I am trying to access.
<div class="loginPanel">
<div class="container">
<div class="title">Login</div>
<form id="loginForm" class="loginForm" method="POST" action="/dis/login">
<font class="portlet-msg-error"
style="font-weight: bold; font-size: 10px; color:#FF0000; text-align: center;"></font>
<table>
<tr>
<td class="label">USERNAME:</td>
<td><input
value=""
class="edit"
id="txtUsernameLogin"
name="txtUserName"
type="text"/></td>
</tr>
<tr>
<td class="label">PASSWORD:</td>
<td><input
value=""
class="edit"
name="txtPassword"
type="password"/></td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="checkbox" name="txtRemember" value="true" align="middle"/>
Remember me on this computer
</td>
</tr>
<tr>
<td></td>
<td class=""><input type="submit"
class="submit"
value="Login"
alt="Login"/></td>
</tr>
</table>
</form>
</div>
</div>
It appears you are posting to /dis/login.jsp whereas the form posts to /dis/login
It also may be because of security features employed by the website. Try setting a referrer, user-agent, etc in Jsoup.

Categories

Resources