So this is my first question here after reading like hundreds of threads in this forum :D.
I want to click an HtmlAnchor with HtmlUnit and retrieve the underlying url.
Maybe I am using the wrong methods or something, but here is the code that I tried so far:
public List<String> clickURLs(String searchAttribute) {
try {
wc.getOptions().setCssEnabled(false); wc.getOptions().setJavaScriptEnabled(false); wc.getOptions().setUseInsecureSSL(true); wc.getOptions().setPrintContentOnFailingStatusCode(false); wc.getOptions().setThrowExceptionOnFailingStatusCode(false); wc.getOptions().setThrowExceptionOnScriptError(false);
HtmlPage page = wc.getPage(url);
List<HtmlAnchor> links =page.getAnchors();
for (HtmlAnchor link: links) {
if(link.getTextContent().contains(searchAttribute)) {
String href =link.click().getWebResponse().toString();
System.out.println(href);
list.add(href);
}
}
} catch (Exception e) {
System.out.println("An error occured: " + e);
} // catch
finally {
wc.close();
} // finally
return list;
} // clickURLs()
Any help would be appreciated.
Related
I am trying to understand why my code won't run.
The example is how to use java.io to find a path directory and print out the title of all .txt files in that directory.
My code is below:
try
{
Files.walk(Paths.get(\\Users\\Name\\Desktop\\Test Folder)).forEach(p -> {
if (p.getFileName().toString().endsWith(".txt")) {
System.out.println("Text doc: " + p.getFileName());
}
});
} catch (IOException e) {
e.printStackTrace();
}
I am currently importing java.io.IOException, java.nio.file.Files, and java.nio.file.Paths.
The error I am getting is java.nio.file.NoSuchFileException: myDirPath.
If anyone can help, or at least point me in the right direction I would be very grateful.
try
{
Files.walk(Paths.get("/Users/Jayden/Desktop/Test Folder")).forEach(p -> {
if (p.getFileName().toString().endsWith(".txt")) {
System.out.println("Text doc: " + p.getFileName());
}
});
} catch (IOException e) {
e.printStackTrace();
}
I was not using slashes properly.
I'm trying to automatize some test in order to open Jstree Nodes, using the Node name
I would like to make it clicking the arrow at the left of the node, instead of double-clicking the node itself.
Here is the code:
public void abrirSistema(String node) {
Boolean isPresent = driver.findElements(By.xpath("//*[contains(text(),'" + node + "')]")).size() > 0;
if (isPresent) {
System.out.println("System está abierto");
} else {
System.out.println("Hay que abrir el sistema");
WebElement system = driver.findElement(By.xpath("//*[contains(text(),'" + node + "')]"));
WebElement parent = system.findElement(By.xpath(".."));
String parentId = parent.getAttribute("id");
WebElement flecha = driver.findElement(By.xpath("//*[#id=\"" + parentId + "\"]/i"));
// WebElement arrow = parent.findElement(By.className("jstree-icon"));
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
js.executeScript("arguments[0].scrollIntoView(true);", arrow);
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
wait.until(ExpectedConditions.visibilityOf(flecha));
wait.until(ExpectedConditions.elementToBeClickable(flecha)).click();
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
So basically I locate the Node by the name, cause elements on the tree changes dynamically, then pick the arrow, and click it.
The thing is that sometimes the code works, sometimes it doesn't, and I cannot figure it out why.
I make sure that page is full loaded before trying to run this, and when I run the Scenario, the step is always green although the Node is not opened
Also I would like to let you know that this code runs always after opening the root node, which is working properly. Just in case
Hope someone could help me.
Thanks in advance
my program crash when user inserts a weird url
It should go like
while(condition) {
try {
String url = reciveURL();
Document rss = Jsoup.connect( url ).get();
}
catch (MalformedURLException e) {
System.err.println("Invalid URL");
} catch (OthersExceptions e){
Others.Actions();
}
}
The problem is, this throws "java.net.MalformedURLException: no protocol", instead of printing "Invalid URL" and the program crash (when user inserts any other kind of text)
Thanks!
Lol, solved myself, but i'll let the question as theres no post on the same issue
You should import java.net.url, this brings the "URL" type, which triggers the MalformedURLException (Jsoup doesnt do this)
So it goes like this
while(true){
{
String url = reciveURL() ;
URL chk_url = new URL(url);
Document rss = Jsoup.connect( url ).get();
} catch (MalformedURLException e) {
System.err.println("url mal puesta!");
}
}
OS: Windows 7 32bit
ChromeDriver version: 2.30
Selenium Webdriver version: 3.4.0
Java 8
I've tried a few different ways to clean this code up and not have to repeat the same try/catch blocks. I'm trying to check that various elements are present on a page I'm testing. I can gracefully report to the console and this code does work with no problems.
The issue I'm having is with the ungraceful code. Is there a way to nest these try/catch blocks, or put them inside of an if/else loop?
try {
driver.findElement(By.xpath("/html/head/title"));
System.out.println("Title found...");
Thread.sleep(1000);
} catch (NoSuchElementException ex) {
System.out.println("Title NOT FOUND...");
// ex.printStackTrace();
}
try {
driver.findElement(By.xpath("//*[#id='logindiv']"));
System.out.println("Login form found...");
Thread.sleep(1000);
} catch (NoSuchElementException ex) {
System.out.println("Login form NOT FOUND...");
// ex.printStackTrace();
}
try {
driver.findElement(By.id("username"));
System.out.println("'Username' field found...");
Thread.sleep(1000);
} catch (NoSuchElementException ex) {
System.out.println("'Username' form NOT FOUND...");
// ex.printStackTrace();
}
try {
driver.findElement(By.id("password"));
System.out.println("'Password' field found...");
Thread.sleep(1000);
} catch (NoSuchElementException ex) {
System.out.println("'Password' form NOT FOUND...");
// ex.printStackTrace();
}
try {
driver.findElement(By.id("loginSubmit")).getText();
System.out.println("Login button found...");
Thread.sleep(1000);
} catch (NoSuchElementException ex) {
System.out.println("Login button NOT FOUND...");
// ex.printStackTrace();
}
A few things...
I'm a firm believer that exceptions should be exceptional, meaning exceptions shouldn't be used as flow control. This is further supported by the docs,
findElement should not be used to look for non-present elements, use findElements(By) and assert zero length response instead.
so you should replace .findElement() and try-catch with .findElements() and test for empty. See example in #2.
You really should use some Assert library like JUnit, etc. It makes these validations so much easier/cleaner.
This whole thing
try
{
driver.findElement(By.id("username"));
System.out.println("'Username' field found...");
Thread.sleep(1000);
}
catch (NoSuchElementException ex)
{
System.out.println("'Username' form NOT FOUND...");
// ex.printStackTrace();
}
should/could look like
Assert.assertFalse(driver.findElements(By.id("username")).isEmpty(), "Validate Username field exists");
Arguments could be made that it's faster, so on and so forth but... it hurts me to see people use something complicated like an XPath to do no more than locate an element by ID, e.g. By.xpath("//*[#id='logindiv']"). This is so much simpler and easier to read as By.id("logindiv").
You can do some googling to see all the details but Thread.Sleep() is a bad practice and should be avoided. Instead use WebDriverWait. Explore ExpectedConditions to see all what can be waited for and use it liberally. In the cases you posted in your code, I don't see any reason to wait for any of these so that's several seconds of unnecessary wasted time.
Your last example is pulling .getText() but not using it. Since you are just checking that the button exists, you can safely remove the .getText() from the end.
You can try this:
public void checkElementVisibile()throws InterruptedException {
element = driver.findElement(By.xpath("/html/head/title"));
Thread.sleep(1000);
if(isElementVisibile(element))
System.out.println("Title found...");
else
System.out.println("Title not found...");
element = driver.findElement(By.xpath("//*[#id='logindiv']"));
Thread.sleep(1000);
if(isElementVisibile(element))
System.out.println("Login form found...");
else
System.out.println("Login form not found...");
element = driver.findElement(By.id("username"));
Thread.sleep(1000);
if(isElementVisibile(element))
System.out.println("'Username' field found...");
else
System.out.println("'Username' field not found...");
element = driver.findElement(By.id("password"));
Thread.sleep(1000);
if(isElementVisibile(element))
System.out.println("'Password' field found...");
else
System.out.println("'Password' field not found...");
}
public static boolean isElementVisibile(WebElement element){
try {
return element.isDisplayed();
}catch (NoSuchElementException ex)
{
return false;
}
}
Q : How to resume code operation after a handled exception ?
i now using try and catch.
Try
{
Process url and render the text and save contents to text file.
}Catch(Exception ex)
}
Some urls are broken, so how do i skip broken urls and continue with other urls ?
Depends on how you iterate over your URLs. For example:
for (URL url: urllist) {
try {
// Process **one** url
} catch (Exception ex) {
// handle the exception
}
}
This will process all urls in the list, even if some of the processing raises an exception.
That's it - do nothing (apart from perhaps logging a warning), and the code execution will continue. Ex:
for (String url : urls) {
try {
// parse, open, save, etc.
} catch (Exception ex) {
log.warn("Problem loading URL " + url, ex);
}
}
Try this:
for (url : allUrls) {
try {
Process url and render the text and save contents to text file.
} catch(Exception ex) {
...
continue;
}
}
There is a logical error in this pseudo-code.
Think about it like this. Your 'process url' was a loop yes? When it found an exception it exited the process loop to the catch block and then to the end of the algorithm.
You need to nest the entire try catch block in the process loop. That way when it hits an exception it returns to the begging of the loop and not to the end of the program.
Create two methods like this:
public void processAllURLs(final List<String> urls){
for(String url: urls){
processURL(url);
}
}
public void processURL(final String url){
try {
// Attempt to process the URL
} catch (Exception ex) {
// Log or ignore
}
}