1

How can I add 'starts-with' or 'contains' for

(By.xpath("//ul/li[1]")));

something like

(By.xpath("//ul/li[1][contains(text(),'"+textConstant+"')]")));  

which doesn't work

Can anyone assist with the proper formatting for this? Thanks

Zodzie
  • 315
  • 1
  • 17
  • I think you need to back up a layer and include the 'li' in your contains, kind of like this person did here: http://stackoverflow.com/questions/861008/xpath-partial-of-attribute-known – Vic F Aug 04 '15 at 01:00

3 Answers3

0

If Only one li tag contains that text then no need to mention index.

You can write like this:

By locator = By.xpath("//ul/li[contain‌​s(., '"+lostText+"')]");
WebElement errorFind = (new WebDriverWait(mypackage.ClassMain.driver, 15)).until(ExpectedConditions.presenceOfElementLocated(locator));
String text = mypackage.ClassMain.driver.findElement(By.xpath("//ul/li[1]")).getText(); 
if(text.equals("Lose") {
   doNext(); 
} else {
   System.out.print("Win \n"); 
   doOther();
}
Bentaye
  • 8,262
  • 4
  • 28
  • 36
Saritha G
  • 2,365
  • 1
  • 13
  • 25
  • Multiple li tag contains this text, need latest or top-most li – Zodzie Aug 04 '15 at 05:24
  • Not really. The top-most li changes divs from one active/visible state to one invisible state via Node app. I need the li that appears at the top for a short time which contains the text, before it gets stored in the bottom, identified as same li, and containing same text. It looks like I need to include the index as well as the `[contains(.,'"+textConstant+"')]` – Zodzie Aug 04 '15 at 06:36
  • You can get top lost li like: By.xpath("//ul/li[1]"). And then check that webElement is enabled or not. No need to check text, b'coz some times that text may not be given to list..right? If it contains text then that will be in enable mode i guess. – Saritha G Aug 04 '15 at 06:39
  • index that appears may contain 2 different text phrases. Which is why I'm picking partial text from one phrase, and saying "if this isn't found, it's the other" (Cannot use entire text-phrase match) – Zodzie Aug 04 '15 at 06:42
  • Then that will work. Are you getting any error? If doesn't work means there maight be other web-element which has same xpath. At that time you can choose your xpath from parent tag of 'ul' – Saritha G Aug 04 '15 at 06:45
  • I am using ("//ul/li[1]") But it does not seem to differentiate between the two text phrases which appear on that li. Only the fact that the correct element (li in index 1) appears. I appreciate ur assistance. – Zodzie Aug 04 '15 at 06:46
  • only one text you want display? or you need both the text's? – Saritha G Aug 04 '15 at 06:49
  • Here is a snippet from my source code: `try { WebElement errorFind = (new WebDriverWait(mypackage.ClassMain.driver, 15)).until(ExpectedConditions.presenceOfElementLocated(By.xpath("//ul/li[contains(., '"+lostText+"')]"))); System.out.print("Lose - " +mypackage.ClassMain.driver.findElement(By.xpath("//ul/li[1]")).getText()+ "\n"); doNext(); } catch (Exception e) { System.out.print("Win - " +mypackage.ClassMain.driver.findElement(By.xpath("//ul/li[1]")).getText()+" \n"); doOther();}}` – Zodzie Aug 04 '15 at 06:51
  • Thanks for edit, after changing your revised answer: `By locator = By.xpath("//ul/li[contain‌​s(., '"+lostText+"')]"); WebElement errorFind = (new WebDriverWait(package.ClassMain.driver, 15)).until(ExpectedConditions.presenceOfElementLocated(locator)); String text = package.ClassMain.driver.findElement(By.xpath("//ul/li[1]")).getText(); if(text.equals("Lose")){ doOne(); } else{ System.out.print("Win \n"); doOther();` ERROR: Timed out after 15 seconds waiting for presence of element located by: By.xpath: //ul/li[contain‌​s(., 'correct text')]"The expression is not a legal expression." – Zodzie Aug 04 '15 at 07:14
  • No. text of the element will never include the word "Lose". It will include in part of the text +lostText+. Right now, it is getting stuck at `waiting for presence of element located by: By.xpath: //ul/li[contain‌​s(., 'lostText')]` when the element did appear on first li . I appreciate your time. – Zodzie Aug 04 '15 at 07:24
  • Try like this WebDriverWait wait = new WebDriverWait(driver, timeout); WebElement element=wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("your xpath"))); – Saritha G Aug 04 '15 at 07:35
0

If textConstant is coming inside li tag, then you can write in following manner:-

(By.xpath("//ul/li[contains(.,'"+textConstant+"')]));

here, dot . stands for text()

Vishal Jagtap
  • 846
  • 9
  • 15
  • Right off the bat I see you are missing the ending quote. Afraid to try this because not sure if it will hit the first instance or be confused when more "li" appear that are identical – Zodzie Aug 04 '15 at 06:26
  • Correct, missed ending quote:- (By.xpath("//ul/li[contains(.,'"+textConstant+"')]")); textConstant string that you are using will be definitely unique..right? If Yes, then it will search only for that "li" which contains string textConstant. – Vishal Jagtap Aug 04 '15 at 08:50
0

You can use the AND operator for the Xpath.

//ul/li[position()=1 and contains(text(),'"+textConstant+"')]

This will match the top most li tagwhich contains the text constant value.

debugger89
  • 706
  • 1
  • 5
  • 11
  • Your answer is one that does not give me "The expression is not a legal expression". Although not listed on Original post, you gathered that the text is not unique and i needed top-most li tag so i will give point to you. Thanks. – Zodzie Aug 04 '15 at 15:44