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 + "']"
Related
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.
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))
I got the following regex working to search for video links in a page
(http(s?):/)(/[^/]+)\\S+.\\.(?:avi|flv|mp4)
Unfortunately it does not stop at the end of the link if there is another match right behind it, for example this video link
somevideoname.avi
would, after regex return this:
http://somevideo.flv">somevideoname.avi
How can I adjust the regex to avoid this? I would like to learn more about regex, its fascinating but so complex!
Here is how you can do something similar with JSoup parser.
Scanner scanner = new Scanner(new File("input.txt"));
scanner.useDelimiter("\\Z");
String htmlString = scanner.next();
scanner.close();
Document doc = Jsoup.parse(htmlString);
// or to get connect of some page use
// Document doc = Jsoup.connect("http://example.com/").get();
Elements elements = doc.select("a[href]");//find all anchors with href attribute
for (Element el : elements) {
URL url = new URL(el.attr("href"));
if (url.getPath().matches(".*\\.(?:avi|flv|mp4)")) {
System.out.println("url: " + url);
//System.out.println("file: " + url.getPath());
System.out.println("file name: "
+ new File(url.getPath()).getName());
System.out.println("------");
}
}
I'm not sure I understand the groupings in your regexp. At any rate, this one should work:
\\bhttps?://[^\"]+?\\.(?:avi|flv|mp4)\\b
If you only want to extract href attribute values then you're better off matching against the following pattern:
href=("|')(.*?)\.(avi|flv|mp4)\1
This should match "href" followed by either a double-quote or single-quote character, then capture everything up to (and including) the next character which matches the starting quote character. Then your href attribute can be extracted by
matcher.group(2) + "." + matcher.group(3)
to concatenate the file path and name with a period and then the file extension.
Your regex is greedy:
Limit its greediness read this:
(http(s?):/)(/[^/]+?)\\S+.\\.(?:avi|flv|mp4)
I have the following URL that I need to get the 4805206 code from.
href="http://adserver.adtech.de/adlink|832|4805206|0|1686|AdId=9624985;BnId=1;itime=527032581;nodecode=yes;link=http://URL/Recruiters/Lex-Consultancy-3979.aspx"
I was wondering if its possible to do this and if so how?
Heres my Java Selenium Class
public void checkAdTechKeys(WebDriver driver) {
if(driver.getCurrentUrl().equalsIgnoreCase("URL"))
{
HP_LeftSearchBox(driver);//enter search terms
driver.get("URL");
// driver.findElement(By.linkText("Read More")).getAttribute("href").toString();
String url = new String(driver.findElement(By.linkText("Read More")).getAttribute("href").toString());
// url = url.split("|")[2];
System.out.println(url);
}else{
setup.loadHomePage(driver);
checkAdTechKeys(driver);
}
}
The code with a small modification that prints out that number:
driver.get("http://irishjobs.ie/");
String url = driver.findElement(By.linkText("Read More")).getAttribute("href");
String[] parsedUrl = url.split("\\|");
System.out.println(parsedUrl[2]);
Two things that you missed:
escaping the "|"
.split() returns an array of strings, not a string.
As the title suggest i only want to replace content which starts # and skip content which starts with ! Here is the code snippet. it is not skipping the word which starts with !#
String test = "Hello #Admin Welcome this is Your welcome page !#Admin This is #Admin"
NOTE:- It must skip !#Admin when replacing.
String out = test.replaceAll("#Admin", "MyAdministrator");
log.debug("OutPut: "+out);
OutPut: Hello MyAdministrator Welcome this is Your welcome page !MyAdministrator This is MyAdministrator
How can i Ignore the word which starts with Exclamation mark.
THANKS.
Use a negative lookbehind?
String test = "Hello #Admin Welcome this is Your welcome page !#Admin This is #Admin";
String out = test.replaceAll("(?<!!)#Admin", "MyAdministrator");
System.out.println("OutPut: "+out);
The lookbehind is (?<!!).
try this
String out = test.replaceAll("(?<!!)#Admin", "MyAdministrator");
this is called negative lookbehind, see Pattern API