1

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:

enter image description here

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?

Jonathan
  • 395
  • 2
  • 8
  • 25
  • I think you are missing quotation marks. You have `driver.findElement(By.xpath("//*[contains(text(),"+client+")]")).click();` and it shout be `driver.findElement(By.xpath("//*[contains(text(),'"+client+"')]")).click();` – Pivoman Mar 06 '18 at 12:13
  • I will try this one minute – Jonathan Mar 06 '18 at 12:15
  • when i used the above i got this message from stack trace ...Unable to locate element: {"method":"xpath","selector":"//*[contains(text(),'midrfrate ')]"} – Jonathan Mar 06 '18 at 12:19
  • Well, you have whitespace in your client variable. Try this `driver.findElement(By.xpath("//*[contains(text(),'"+client.trim()+"')]")).click();` – Pivoman Mar 06 '18 at 12:48
  • I will the that but the white space is from (the stack trace message in eclipse, it put the rest on another line) hence the space – Jonathan Mar 06 '18 at 12:50
  • while i am trying this can i do the same thing with @title driver.findElement(By.xpath(".//*[@title='"+client+"']")).click(); – Jonathan Mar 06 '18 at 12:51
  • @Pivoman THIS WORKED!! ....driver.findElement(By.xpath("//*[contains(text(),'"+client.trim()+"')]")).click(); I must have had a white space but when i think about it its readying from a text file and the words are under each other so there was a white space after each most likely – Jonathan Mar 06 '18 at 12:57
  • I'm glad, summary is in my answer. – Pivoman Mar 06 '18 at 13:06

2 Answers2

1

The string ".//*[(text(), " + client + "]" is equals to ".//*[(text(), midrfrate]"

You need to put the variable in apostrophes

".//*[contains(text(), '" + client + "']"
Guy
  • 46,488
  • 10
  • 44
  • 88
  • I will try this one minute – Jonathan Mar 06 '18 at 12:19
  • This did not work eigther this is the stack trace error .....org.openqa.selenium.InvalidSelectorException: invalid selector: Unable to locate an element with the xpath expression .//*[(text(), 'midrfrate '] because of the following error: SyntaxError: Failed to execute 'evaluate' on 'Document': The string './/*[(text(), 'midrfrate ']' is not a valid XPath expression. – Jonathan Mar 06 '18 at 12:25
  • This is what i have in eclipse ... driver.findElement(By.xpath(".//*[(text(), '" + client + "']")).click(); – Jonathan Mar 06 '18 at 12:26
  • @Jonathan the `contains` is missing. – Guy Mar 06 '18 at 12:27
  • @Jonathan You also have a white space at the end of `'midrfrate '` – Guy Mar 06 '18 at 12:28
  • so no white space is what your saying? – Jonathan Mar 06 '18 at 12:29
  • Okay this is what i put in eclipse driver.findElement(By.xpath(".//*[contains(text(),'"+client+"']")).click(); it still dose not work – Jonathan Mar 06 '18 at 12:33
  • stack trace still says.....Unable to locate an element with the xpath expression .//*[contains(text(),'midrfrate '] because of the following error: SyntaxError: Failed to execute 'evaluate' on 'Document': The string './/*[contains(text(),'midrfrate ']' is not a valid XPath expression. – Jonathan Mar 06 '18 at 12:33
  • I have been beating my head against the wall for the past several days and ran into these same road blocks. – Jonathan Mar 06 '18 at 12:35
  • Is there any other way to do this other than xpath can Linktext be used? – Jonathan Mar 06 '18 at 12:39
  • @Jonathan try to use the tag instead of `*`, for example `"//div[contains(text(),'"+client+"']"` – Guy Mar 06 '18 at 12:51
  • Thanks for helping i found the answer which i can use eigther because i have white space... driver.findElement(By.xpath("//span[contains(text(),'"+client.trim()+"')]")).click(); driver.findElement(By.xpath("//*[contains(text(),'"+client.trim()+"')]")); – Jonathan Mar 06 '18 at 13:08
1

Your xpath there:

driver.findElement(By.xpath("//*[contains(text(),"+client+")]")).click();:

  1. Should contain apostrophes

  2. 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.

Pivoman
  • 4,435
  • 5
  • 18
  • 30