How to fetch data from another website? - java

I am trying to fetch data from a web site and I am able to do this but my problem is that
when I fetch data of different commodity from site then I have to change every time in my code and I want to fetch data of following commodity at once how can I achieve my output
commodity
'Rice'
'Jwar'
'Corn'
'Matar'
'Chana'
Here is my code
import com.gargoylesoftware.htmlunit.BrowserVersion;
import java.util.StringTokenizer;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.openqa.selenium.support.ui.Select;
public class Getdata1 {
public static void main(String args[]) throws {
int j=0;
WebDriver driver = new HtmlUnitDriver(BrowserVersion.getDefault());
String sDate = "27/03/2014";
String commodity="Jo";
String url="http://www.upmandiparishad.in/commodityWiseAll.aspx";
driver.get(url);
Thread.sleep(5000);
new Select(driver.findElement(By.id("ctl00_ContentPlaceHolder1_ddl_commodity"))).selectByVisibleText(commodity);
driver.findElement(By.id("ctl00_ContentPlaceHolder1_txt_rate")).sendKeys(sDate);
Thread.sleep(3000);
driver.findElement(By.id("ctl00_ContentPlaceHolder1_btn_show")).click();
Thread.sleep(5000);
WebElement findElement = driver.findElement(By.id("ctl00_ContentPlaceHolder1_GridView1"));
// WebElement find=driver.findElement(By.id("ctl00_ContentPlaceHolder1_ddl_commodity"));
String htmlTableText = findElement.getText();
htmlTableText=htmlTableText.replace("S.No.DistrictMarketPrice","");
htmlTableText= htmlTableText.replaceAll("\\s(\\d+\\s[A-Z])", "\n$1");
htmlTableText = htmlTableText.replaceAll("(?=(.*?[ ]){4,}).*?[\n\r]", "");
System.out.println(htmlTableText);
driver.close();
driver.quit();
}
}

You perhaps need commodity sent as command line argument or as input from user.

Related

I am trying to Copy and paste OTP using java selenium but it's not working

I am trying to Copy and paste OTP using java selenium but it's not working I am using xpath to copy OTP from yopmail using java selenium I am unable to verify if the OTP has been copied successfully or not
package Amb;
import graphql.schema.idl.SchemaGenerator;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.util.concurrent.TimeUnit;
public class Loginusingtempmail {
public static void main(String[] args) throws AWTException {
System.setProperty("webdriver.chrome.driver", "/home/gt8201/Automation Tools /Chromedriver/chromedriver_linux64 (3)/chromedriver_linux64 (4)/chromedriver");
WebDriver driver = new ChromeDriver();
driver.get("https://yopmail.com/en/wm");
driver.manage().window().maximize();
driver.navigate().forward();
driver.manage().timeouts().implicitlyWait(8, TimeUnit.SECONDS);
driver.findElement(By.xpath(" //*[#id=\"login\"]")).sendKeys("bony#yopmail.com");
String windowHandle = driver.getWindowHandle();
//ArrayList<String> tabs = new ArrayList<String>(driver.getWindowHandles());
//driver.switchTo().window(tabs.get(1));
driver.findElement(By.xpath("/html/body/div/div[2]/main/div[3]/div/div[1]/div[2]/div/div/form/div/div/div[4]/button/i")).click();
driver.navigate().forward();
//String strCode = driver.findElement(By.tagName("tr")).getText();
//driver.findElement(By.xpath(" /html/body/div[2]/div[4]/button/div[1]/span[2]")).click();
driver.navigate().forward();
String OTP = driver.findElement(By.xpath(" //*[#id=\"ifinbox\"]")).getText();
driver.findElement(By.xpath("//*[#id=\"ifinbox\"]")).sendKeys(OTP);
Actions actions = new Actions(driver);
WebElement Text = driver.findElement(By.xpath("//*[#id=\"ifinbox\"]"));
actions.moveToElement(Text).doubleClick().build().perform();
driver.findElement(By.xpath("//*[#id=\"ifinbox\"]")).sendKeys(Keys.chord(Keys.CONTROL,"c"));
driver.navigate().forward();
driver.findElement(By.xpath("//*[#id=\"ifinbox\"]")).sendKeys(Keys.chord(Keys.CONTROL,"v"));
System.out.println("The header text is - " +Text.getText());
}
}
[1]: https://i.stack.imgur.com/z3q2C.png

Is it possible to send keys in Selenium via commad line argument in console not via hardcoding the throught sendkeys("name")?

Hey all I have a question.
My assignment is that my program takes a command-line argument and then prints out the data.
I managed to navigate to the desired page and also print out the data I need but I did it with .sendKeys("name i need here"). Is it possible that at the start there would be like a question "what name do you want to type in" and you would then type the name in the command-line and everything else would play out the same?
Here's my code:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.By.ByXPath;
import org.openqa.selenium.chrome.ChromeDriver;
import java.io.IOException;
import java.util.List;
public class Scraper {
public static void main( String[] args ) throws IOException
{
System.setProperty("webdriver.chrome.driver", "D:/Chrome/chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://www.basketball-reference.com/");
driver.findElement(By.xpath("//*[#id='header']/div[3]/form/div/div/input[2]")).sendKeys("Luka Dončić");
driver.findElement(ByXPath.xpath("//*[#id='qc-cmp2-ui']/div[2]/div/button[3]")).click();
driver.findElement(ByXPath.xpath("//*[#id='header']/div[3]/form/input[1]")).click();
driver.findElement(ByXPath.xpath("//*[#id='players']/div[1]/div[1]/strong/a")).click();
WebElement element = driver.findElement(By.id("div_per_game"));
List<WebElement> trojke = element.findElements(By.cssSelector("td[data-stat='fg3a_per_g']"));
List<WebElement> sezona = element.findElements(By.xpath("//*[#id='per_game']/tbody/tr/th/a"));
for (WebElement s: sezona)
{
System.out.println(s.getText());
}
for (WebElement t : trojke)
{
System.out.println(t.getText());
}
}
}
So instead of sendKeys("Luka Dončić") that automatically puts the name in the search box of a page I want to write the name myself in command line and when I press enter or something the rest would execute the same. Is it possible?
Here's the page I want to send the name to in the search box: https://www.basketball-reference.com/
Oh yes and I'm using Java with Selenium and Maven.
Before sending the text to the web element you can read the user input from console
import java.util.Scanner;
Scanner my_scan = new Scanner(System.in);
String my_text = my_scan.nextLine();
so now you can send the my_text
driver.findElement(By.xpath("//*[#id='header']/div[3]/form/div/div/input[2]")).sendKeys(my_text);
the entire code will look like this:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.By.ByXPath;
import org.openqa.selenium.chrome.ChromeDriver;
import java.io.IOException;
import java.util.List;
import java.util.Scanner;
public class Scraper {
public static void main( String[] args ) throws IOException
{
System.setProperty("webdriver.chrome.driver", "D:/Chrome/chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://www.basketball-reference.com/");
Scanner my_scan = new Scanner(System.in);
String my_text = my_scan.nextLine();
driver.findElement(By.xpath("//*[#id='header']/div[3]/form/div/div/input[2]")).sendKeys(my_text);
driver.findElement(ByXPath.xpath("//*[#id='qc-cmp2-ui']/div[2]/div/button[3]")).click();
driver.findElement(ByXPath.xpath("//*[#id='header']/div[3]/form/input[1]")).click();
driver.findElement(ByXPath.xpath("//*[#id='players']/div[1]/div[1]/strong/a")).click();
WebElement element = driver.findElement(By.id("div_per_game"));
List<WebElement> trojke = element.findElements(By.cssSelector("td[data-stat='fg3a_per_g']"));
List<WebElement> sezona = element.findElements(By.xpath("//*[#id='per_game']/tbody/tr/th/a"));
for (WebElement s: sezona)
{
System.out.println(s.getText());
}
for (WebElement t : trojke)
{
System.out.println(t.getText());
}
}
}

When I am using SendKeys to send the date in the field and when I am running the script its pasting the date twice in that field

package debut.Ifinca.NewOrder;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;
public class NewIfincaOrder {
public static void main(String[] args)
{ System.setProperty("webdriver.chrome.driver","C:\\\\Users\\\\COMPUTER\\\\Desktop\\\\chromedriver_win32 (2)\\\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://js-server.debutinfotech.com:3093/");
driver.manage().window().maximize();
//Here When I am sending this date its getting entered twice in this field.This field has Date picker format.
driver.findElement(By.xpath(".//input[#id='exporter_delivery_date']")).sendKeys("02/18/2020");
}
}
Have you tried clearing the field first?
driver.findElement(By.xpath(".//input[#id='exporter_delivery_date']")).clear();
driver.findElement(By.xpath(".//input[#id='exporter_delivery_date']")).sendKeys("02/18/2020");

Selenium- The method is undefined for the type class. Exception during Java Class extension

I am new to selenium and java and trying to build a program and facing multiple issues. Listed is the code below for Parent Class.
Login Method error.
Void is an valid type error on Syntax error on token "(".
Even though i tried to change, still i face an error
package MyfirstMavenProject.Myfirstgmailtest;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class LoginClass {
//Open the Browser
public void BrowserOpen (String args[]) {
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
//Get the URL
driver.navigate().to("https://accounts.google.com/ServiceLogin?service=mail&passive=true&rm=false&continue=https://mail.google.com/mail/&ss=1&scc=1&ltmpl=default&ltmplcache=2&emr=1&osid=1#identifier");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
//Identifies the gmail and password
public void Login (String args[]) {
WebElement emailfield = driver.findElement(By.id("Email"));
emailfield.sendKeys("abc.com");
driver.findElement(By.id("next")).click();
WebDriverWait wait = new WebDriverWait(driver,30);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[#type='password']"))).sendKeys("abc");
driver.findElement(By.id("signIn")).click();
}
}
}
Child Class is where i am getting error on argument. Need info as to what argument should i pass. I am trying to use the Login method created in the above class
package MyfirstMavenProject.Myfirstgmailtest;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class ComposeEmailClass extends LoginClass {
//Method to identify the compose email
public void ComposeEmail (String args[]){
WebDriver ComposeEmail = new FirefoxDriver();
ComposeEmail.findElement(By.className("T-I J-J5-Ji T-I-KE L3")).click();
}` public static void main (String args[]){
ComposeEmailClass ClickCompose = new ComposeEmailClass();
ClickCompose.Login(args);`\\Need more info`
ClickCompose.ComposeEmail(args);
}FireFox.Quit;
}
Use following code:
There are lots of syntax error present in your code.
Login Class: Corrected
package MyfirstMavenProject.Myfirstgmailtest;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class LoginClass
{
WebDriver driver =null;
//Open the Browser
public void BrowserOpen (String args[])
{
driver = new FirefoxDriver();
driver.manage().window().maximize();
//Get the URL
driver.navigate().to("https://accounts.google.com/ServiceLogin?service=mail&passive=true&rm=false&continue=https://mail.google.com/mail/&ss=1&scc=1&ltmpl=default&ltmplcache=2&emr=1&osid=1#identifier");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
}
//Identifies the gmail and password
public void Login (String args[])
{
WebElement emailfield = driver.findElement(By.id("Email"));
emailfield.sendKeys("youremail#gmail.com");
driver.findElement(By.id("next")).click();
WebDriverWait wait = new WebDriverWait(driver,30);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//* [#type='password']"))).sendKeys("password");
driver.findElement(By.id("signIn")).click();
}
}
ComposeEmailClass: Corrected
package MyfirstMavenProject.Myfirstgmailtest;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class ComposeEmailClass extends LoginClass
{
//Method to identify the compose email
public void ComposeEmail(String args[])
{
WebDriver ComposeEmail = new FirefoxDriver();
ComposeEmail.findElement(By.className("T-I J-J5-Ji T-I-KE L3")).click();
}
public static void main(String args[])
{
ComposeEmailClass ClickCompose = new ComposeEmailClass();
ClickCompose.BrowserOpen(args);
ClickCompose.Login(args);
ClickCompose.ComposeEmail(args);
}
}
You have to Call ClickCompose.BrowserOpen(args); before ClickCompose.Login(args);
and String[] args is not required in your method declaration .

Output of the webdriver program is not coming in the right format(tabular way)

import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class WebTable_Dynamic {
public static void main(String[] args) {
WebDriver driver= new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.get("http://www.w3schools.com/tags/tag_table.asp");
WebElement element=driver.findElement(By.xpath("//table[#class='reference notranslate']"));
List<WebElement> rows=element.findElements(By.tagName("tr"));
for(int rowNum=1;rowNum<rows.size();rowNum++){
List<WebElement> cells=rows.get(rowNum).findElements(By.tagName("td"));
for(int colNum=0;colNum<cells.size();colNum++){
System.out.print(cells.get(colNum).getText());
System.out.print("----");
}
System.out.println();
}
driver.close();
}
}
My program is extracting the elements from a table.
Currently I am not getting the output in a tabular way due to line break <br> tags in the cell content. Can someone guide me in getting the o/p in a tabular way?

Categories

Resources