i'm trying to work with a little android at the moment.
Before all the hate - Yes I have tried searching and found answers related to mine but I simply couldnt get mine to work with the way they did it :/.
I have found out that Jsoup is rather good for parsing data from HTML to use in an app.
So I have trying to recieve data from here Krak
So when I enter the input for a number lets say "86202710"
The link will be Number link
I have then tried to extract the name of the owner of the given number which is "Jens Fisker Automobiler A/S". But I cant seem to get this text out into my textview.
Hope you guys can guide me alittle...
I get an exception "NetworkOnMainThreadException" - AndroidBlockGuardPolicy.onNetwork"
Here is the code I have written to the method for extracting the owner of the number
public void getData() throws IOException{
URL url = new URL("http://mobil.krak.dk/h/#companyResult&searchWord=86202710");
Document doc = Jsoup.parse(url, 3000);
Element content = doc.select("p[header bold]").first();
text = (TextView) findViewById(R.id.tv);
text.setText(content.text());
}
You have to run your networkcode in a AsyncTask on Android. Everything else will fail.
See here: Error when parsing Html using Jsoup
Do you connect through a proxy?
Please check if you have the settings like posted here: Android NetworkOnMainThreadException inside of AsyncTask
Btw. better use connect() method than parse() for such connections:
public void getData() throws IOException{
// You can use a simple string as url
final String url = "http://mobil.krak.dk/h/#companyResult&searchWord=86202710";
// Connect to url and parse it's content
Document doc = Jsoup.connect(url).get(); // Timeout is set to 3 Sec. per default
// Everything else stays same
Element content = doc.select("p[header bold]").first();
text = (TextView) findViewById(R.id.tv);
text.setText(content.text());
}
Related
I'm trying to add an xml payload as a string to a text field on a website, using Selenium's sendKeys. My issue is that my code is progressing to the next line before all of my xml string has been entered into the text field. This is what my current test looks like;
#FindBy(id = "macro-param-graph")
private WebElement graphIdXml;
public void invokeGraphXmlPayload(String payload) throws IOException, InterruptedException {
String myGraph = generateStringFromResource("src/test/resources/testDataResources/" + payload);
WebDriverWait wait = new WebDriverWait(driver, 15);
wait.until(ExpectedConditions.visibilityOf(graphIdXml));
graphIdXml.sendKeys(myGraph);
btn_Preview.click();
}
I've tried something as simple as trying a Thread.sleep just to see if that helps, but seeing as that happens after Selenium has already done the sendKeys function it hasn't been much help.
I'm not receiving an error message per se, just that the rest of the tests continue to run while the xml string is still being sent, so it just continues while making a mess.
Any help or advice on this would be appreciated.
Removing the xml formatting of the xml file I was converting to string has resolved this issue.
I think it's a case where it was inputting returns in the string when the file had the default xml format. The code in the question above now works without any edits.
I want to ask how to get the text from element where didnt have unic id/class, i tried using xpath(copied from web browser) but it's not working this is the picture.
try below code;
("//*[#class='col-sm-4']//*[#class='text-grey'][1]").getText();
This might help.
Document doc = Jsoup.parse(driver.getPageSource());
Elements content = doc.select("span[class^=text-grey]");
ArrayList<String> allTextIntextGreyClass = (ArrayList<String>) content.eachText();
Then you can work with ArrayList to get the text you want to work with, or you can work with "content". If this does not work, you can get the inner HTML and work with it to get the right context you want. You can get innerHTML as follows:
String inHTML = driver.findElement(By.className("text-grey")).getAttribute("innerHTML");
How to upload files from local via window prompt using selenium webdriver?
I want to perform the following actions:
click on 'Browse' option on the window
from the window prompt go to the particular location in the local where the file is kept
select the file and click on 'Open' to upload the file.
Have you tried using input() on proper file input control?
WebElement fileInput = driver.findElement(By.id("some id"));
fileInput.sendKeys("C:/path/to/file.extension");
I have used below three different ways to upload a file in selenium webdriver.
First simple case of just finding the element and typing the absolute path of the document into it. But we need to make sure the HTML field is of input type. Ex:<input type="file" name="uploadsubmit">
Here is the simple code:
WebElement element = driver.findElement(By.name("uploadsubmit"));
element.sendKeys("D:/file.txt");
driver.findElement(By.name("uploadSubmit"));
String validateText = driver.findElement(By.id("message")).getText();
Assert.assertEquals("File uploaded successfully", validateText);
Second case is uploading using Robot class which is used to (generate native system input events) take the control of mouse and keyboard.
The the other option is to use 'AutoIt' (open source tool).
You can find the above three examples : - File Uploads with Selenium Webdriver
Selenium Webdriver doesn't really support this. Interacting with non-browser windows (such as native file upload dialogs and basic auth dialogs) has been a topic of much discussion on the WebDriver discussion board, but there has been little to no progress on the subject.
I have, in the past, been able to work around this by capturing the underlying request with a tool such as Fiddler2, and then just sending the request with the specified file attached as a byte blob.
If you need cookies from an authenticated session, WebDriver.magage().getCookies() should help you in that aspect.
edit: I have code for this somewhere that worked, I'll see if I can get ahold of something that you can use.
public RosterPage UploadRosterFile(String filePath){
Face().Log("Importing Roster...");
LoginRequest login = new LoginRequest();
login.username = Prefs.EmailLogin;
login.password = Prefs.PasswordLogin;
login.rememberMe = false;
login.forward = "";
login.schoolId = "";
//Set up request data
String url = "http://www.foo.bar.com" + "/ManageRoster/UploadRoster";
String javaScript = "return $('#seasons li.selected') .attr('data-season-id');";
String seasonId = (String)((IJavaScriptExecutor)Driver().GetBaseDriver()).ExecuteScript(javaScript);
javaScript = "return Foo.Bar.data.selectedTeamId;";
String teamId = (String)((IJavaScriptExecutor)Driver().GetBaseDriver()).ExecuteScript(javaScript);
//Send Request and parse the response into the new Driver URL
MultipartForm form = new MultipartForm(url);
form.SetField("teamId", teamId);
form.SetField("seasonId", seasonId);
form.SendFile(filePath,LoginRequest.sendLoginRequest(login));
String response = form.ResponseText.ToString();
String newURL = StaticBaseTestObjs.RemoveStringSubString("http://www.foo.bar.com" + response.Split('"')[1].Split('"')[0],"amp;");
Face().Log("Navigating to URL: "+ newURL);
Driver().GoTo(new Uri(newURL));
return this;
}
Where MultiPartForm is:
MultiPartForm
And LoginRequest/Response:
LoginRequest
LoginResponse
The code above is in C#, but there are equivalent base classes in Java that will do what you need them to do to mimic this functionality.
The most important part of all of that code is the MultiPartForm.SendFile method, which is where the magic happens.
original url : http://pricecheckindia.com/go/store/ebay/52440?ref=velusliv
redirected url : http://www.ebay.in/itm/Asus-Zenfone-6-A600CG-A601CG-White-16-GB-/111471688863?pt=IN_Mobile_Phones&aff_source=DA
I need a program that will take the original url and print the redirected url.
How to get this done in java.
public static void main(String[] args) throws IOException, InterruptedException
{
String url = "http://pricecheckindia.com/go/store/ebay/52440?ref=velusliv";
Response response = Jsoup.connect(url).followRedirects(false).execute();
System.out.println(response.url());
}
It seems that you are being redirected via JavaScript code, which Jsoup doesn't support (it is simple HTML parser, not browser emulator). Your choice then is to either use tool which will support JavaScript like Selenium web driver, or parse your page to get url from click here link from
If it is taking too long to redirect, then please click here
text.
You can use Jsoup to get this link by adding to your current code
Document doc = response.parse();
String redirectUrl = doc.select("a:contains(click here)").attr("href");
System.out.println(redirectUrl);
which will return and print
http://rover.ebay.com/rover/1/4686-127726-2357-15/2?&site=Partnership_PRCCHK&aff_source=DA&mpre=http%3A%2F%2Fwww.ebay.in%2Fitm%2FAsus-Zenfone-6-A600CG-A601CG-White-16-GB-%2F111471688863%3Fpt%3DIN_Mobile_Phones%26aff_source%3DDA
so now all we need to do is parse query from this URL to get value of mpre key, which encoded version looks like
http%3A%2F%2Fwww.ebay.in%2Fitm%2FAsus-Zenfone-6-A600CG-A601CG-White-16-GB-%2F111471688863%3Fpt%3DIN_Mobile_Phones%26aff_source%3DDA
but after decoding it will actually represents
http://www.ebay.in/itm/Asus-Zenfone-6-A600CG-A601CG-White-16-GB-/111471688863?pt=IN_Mobile_Phones&aff_source=DA
To get value of this key and decode it you can use one of solutions from this question: Parse a URI String into Name-Value Collection. With help of method from accepted answer in previously mentioned question we can just invoke
URL address = new URL(redirectUrl);
Map<String,List<String>> urlQuerryMap= splitQuery(address);
String redirected = urlQuerryMap.get("mpre").get(0);
System.out.println(redirected);
to see result
http://www.ebay.in/itm/Asus-Zenfone-6-A600CG-A601CG-White-16-GB-/111471688863?pt=IN_Mobile_Phones&aff_source=DA
www.rgrfm.be/rgrsite/maxradio/android.php
www.rgrfm.be/rgrsite/maxradio/onair.txt
The track information of the music being played is contained in onair.txt. android.php is a php script I wrote.
I need to display the track information in my Android application. I do not want do download it to disk but keep it in memory. I don't know if the php script is useless because it would create additional overhead. So it's probably better to simply parse onair.txt
InputStream is = new URL("http://www.rgrfm.be/rgrsite/maxradio/onair.txt").openStream();
I am stuck with this. Has anyone got time to help me?
As described, php script seems useless. Since, you can directly read the text file. So, first read it as text, then parse it.
URL url = new URL("http://www.rgrfm.be/rgrsite/maxradio/onair.txt");
String text = readAsText(url)
parse(text);
String readAsText(URL url) {
// read the url as text here.
}
void parse(String text) {
}