using hyperlinks in java file - java

I am trying to add a hyperlink in java file.
TestHyperlink.java
class TestHyperlink.java {
String url = "stackoverflow.com/questions/ask";
String someVariable = "testUrl";
Html entryLink = new Html("<a target=\"_blank\" href=url>someVariable</a>");
}
I am trying to use two string variables url and someVariable but I am not sure how to do it. My hyper link appears as 'someVariable' and on click leads to a broken page.
What I seek is a hyperlink which appears as testUrl and on click leads to a desired url page, stackoverflow.com/questions/ask in this case.
Thanks,
Sony

Java doesn't interpolate variables inside Strings. You need to change to new Html("<a target=\"_blank\" href=\"" + url + "\">" + someVariable + "</a>");

Related

Need to get value after Domain name in url using java

We are getting url from JSON Response and which we open in in Chrome.The page loads , there is submit button which we click then it redirect to url as :-
https://www.google.com/AB1234
We need the need to retrieve only "AB1234" value from url.
tried following code to get value ="AB1234"
String url = driver.getCurrentUrl();
int index=url.lastIndexOf("/");
String result = url.substring(0,index);
but here getting initial part of url:https://www.google.com/
You need to call substring function with index +1 .
Try below code :
String url = driver.getCurrentUrl();
int index = url.lastIndexOf("/");
String result = url.substring(index + 1);
To parse a URI, it's likely a good idea to use a URI parser.
Given http://example.com/bar
String path = URI.create(driver.getCurrentUrl()).getPath();
will get you '/bar'.
Given http://example.com/bar/mumble the same code gets '/bar/mumble'. It's unclear from your question whether this is what you want. Nevertheless, you should at least start the parse as above.

String Variable with Find Element Commands Java

Hello all the following codes works here it is.
String content = FileUtils.readFileToString(new File("C:\\PayrollSync\\prepayroll.txt"));
String [] Arrayclients = content.split("\n");
// begin for loop
for(String client : Arrayclients) {
PP_OBJ_CycleData.verifysearch(driver);
PP_OBJ_CycleData.searchbox(driver).clear();
PP_OBJ_CycleData.searchbox(driver).sendKeys(client);
PP_OBJ_CycleData.searchbox(driver).sendKeys(Keys.BACK_SPACE);
Thread.sleep(4000);
//WebElement dropdown = driver.findElement(By.xpath(".//*[#title="+client+"]"));
//dropdown.click();
//driver.findElement(By.xpath(".//*[(text(),"+client+"]")).click();
driver.findElement(By.xpath("//*[contains(text(),"+client+")]")).click();;
Thread.sleep(2000);
PP_OBJ_CycleData.practitioner(driver).click();
The Problem:
None for the driver.findElements are working such as:
//WebElement dropdown = driver.findElement(By.xpath(".//*[#title="+client+"]"));
//dropdown.click();
//driver.findElement(By.xpath(".//*[(text(),"+client+"]")).click();
driver.findElement(By.xpath("//*[contains(text(),"+client+")]")).click();;
Thread.sleep(2000);
My failure traces says cannot find element By.xpath("//*[contains(text(),"+client+")]")). However I am waiting for the element to be visible when i go to the next page. My wait is below.
Wait<WebDriver> wait = new FluentWait<WebDriver>(driver).withTimeout(20, TimeUnit.SECONDS);
element = driver.findElement(By.id("toolbarQuickSearch"));
wait.until(ExpectedConditions.or(
ExpectedConditions.visibilityOf(element),
ExpectedConditions.elementToBeClickable(element)
));
The problem from what I can see if that after the variable that contains the string is entered into the search field and the string name is visible selenium cant find the string name to click on for example below:
I could put this in and it will click on it all the time:
driver.findElement(By.xpath("//*[text()='midrfrate']"]"));
However if i do something like this it cant find it:
driver.findElement(By.xpath(".//*[(text(),"+client+"]")).click();
driver.findElement(By.xpath("//[contains(text(),"+client+")]")).click();
Please help me I have tried the following with no success:
How to use variables in XPath?
Your xpath there:
driver.findElement(By.xpath("//*[contains(text(),"+client+")]")).click();:
Should contain apostrophes
Text in contains block should contain exact String (you have trailing whitespace in client variable 'midrfrate ').
Final xpath is:
driver.findElement(By.xpath("//*[contains(text(),'"+client.trim()+"')]")).click();
where trim() method removes leading and trailing whitespace from client variable.
The string ".//*[(text(), " + client + "]" is equals to ".//*[(text(), midrfrate]"
You need to put the variable in apostrophes
".//*[contains(text(), '" + client + "']"

Selenium(Java), java.lang.AssertionError

I wrote a method to load the page navigation links. The method works, but when I added code to check the correct URL and tab title my test is not performed. Sometimes it happens that for loop fast clicks on the pages the side that does not get loaded, I do not know whether it is a problem but I can not check whether a page loaded with the correct url or tab title, or the problem is the code that I wrote for check the correct url or tab title.
This is my method:
public void showNavigationLinks(){
Actions action = new Actions(driver);
String[] submenus = {"Accessories", "iMacs", "iPads" , "iPhones" , "iPods" , "MacBook"};
for(int i=0;i<submenus.length;i++)
{
WebElement we = driver.findElement(By.xpath("//a[contains(.,'Product Category')]"));
wait(2000);
action.moveToElement(we).moveToElement(driver.findElement(By.xpath("//a[contains(.,'"+submenus[i]+"')]"))).click().build().perform();
wait(3000);
waitForElementToBeDisplayed(driver.findElement(By.xpath("//a[contains(.,'"+submenus[i]+"')]")) , 500);
Assert.assertTrue(driver.getCurrentUrl().toLowerCase().contains(submenus[i]));
Assert.assertTrue(driver.getTitle().contains(submenus[i]));
}
link_all_product.click();
}
This is my error:
java.lang.AssertionError
at org.junit.Assert.fail(Assert.java:86)
at org.junit.Assert.assertTrue(Assert.java:41)
at org.junit.Assert.assertTrue(Assert.java:52)
at PageObject.ProductPage.showNavigationLinks(ProductPage.java:627)
One of your asserts is returning false, so your current title or url doesn't contain submenus[i]
You're converting the URL to lowercase here (driver.getCurrentUrl().toLowerCase()), but you're comparing it to your submenus, which isn't lowercase. This is probably your problem. Here is the fix:
String expected = submenus[i].toLowerCase();
String actualUrl = driver.getCurrentUrl().toLowerCase();
Assert.assertTrue(actualUrl.contains(expected));
For debugging purposes, you can step through your code to see what's happening, and/or you can make your error more meaningful:
Assert.assertTrue("Expected: " + actualUrl + " to contain: " + expected,
actualUrl.contains(expected))

java / jsoup - retrieve language

i use jsoup to crawl content from specific website´s.
Example, meta-tags:
String meta_description = doc.select("meta[name=description]").first().attr("content");
What i need to crawl as well is the language, what i do:
String meta_language = doc.select("http-equiv").first().attr("content");
But what is thrown:
java.lang.NullPointerException
Anybody could help with with this?
Greetings!
Try this:
String meta_language = doc.select("meta[name=http-equiv]").get(0).attr("content");
System.out.println("Meta description : " + meta_language);
However if you have a list of content in your meta tag then you can use this :
//get meta keyword content
String keywords = doc.select("meta[name=keywords]").first().attr("content");
System.out.println("Meta keyword : " + keywords);

Jsp : How to pass dynamic variable using a hyperlink

I want to pass a (dynamic) variable through a hyperlink.
sname = "my image location";
out.println("hyperlink targeted_url name = sname ");
on targeted_url :
myvalue = request.getParameter("name").toString();
But whenever I tried to output that location, the output show me sname.
Is there any solution? I don't want to use session, cookie or form.
Is it possible within hyperlink?
You're not evaluating sname in your println. This will do that:
out.println("hyperlink targeted_url name = " + sname);
You haven't shown where you are creating your link, but when doing that you need to do the same thing - append the sname variable instead of including it as a string literal.

Categories