0

I am examining two potentially different pages for a user name (verifying text is there). Like this:

1: <div>billgates@starbucks.com</div>

2: <span>billgates@starbucks.com</span>

I am using Java and selenium. In my java function right now I have my

method( String type, String user) and then do

`if (type.equals("Vendor") ) {
   xpath = "//div[contains(text(),'USERNAME')]".replace("USERNAME", username);
} else {
    xpath = "//span[contains(text(),'USERNAME')]".replace("USERNAME", username);
}`

This is fine but I would like to be able to put them together, something like //(div OR span)[contains(text(),'USERNAME')]".replace("USERNAME", username);

Is there a good way to do this? And yes I can say

`"//div[contains(text(),'" + username + "')]"` (or span)

but that confuses me more opening and closing quotes.

user3257891
  • 275
  • 1
  • 4
  • 13
  • I don't want to use "//*" because in the previous screen you enter the username in a box and this would find the //input also – user3257891 Feb 09 '15 at 17:34

2 Answers2

1

If the element you are looking for is either a div or span element, use the following expression:

xpath = "//*[self::div or self::span][contains(text(),'USERNAME')]".replace("USERNAME", username);
Mathias Müller
  • 22,203
  • 13
  • 58
  • 75
  • @user3257891 You are welcome. Let me know if it works. And please have another look at your previous questions! Some of them have [**answers that you should accept**](http://stackoverflow.com/help/someone-answers). – Mathias Müller Feb 09 '15 at 18:46
  • This is the right way, within XPath 1.0, to select "div or span". +1 – LarsH Feb 09 '15 at 20:10
-1

You could use operators. Check more here - http://www.w3schools.com/xpath/xpath_operators.asp.

if("//div[contains(text(),'USERNAME')]" | "//span[contains(text(),'USERNAME')]") {
    //do stuff
}

Edit1

Selenium documentation on xpath, it uses browsers native xpath engine wherever possible - http://docs.seleniumhq.org/docs/03_webdriver.jsp#by-xpath

nilesh
  • 14,131
  • 7
  • 65
  • 79
  • XPath operators are operators _inside_ an XPath expression. Are you sure Selenium accepts such a syntax? (I did not downvote btw.) – Mathias Müller Feb 09 '15 at 18:19
  • @MathiasMüller not necessarily. If you check the link I provided, it does mention this example on the first line of the table `//book | //cd` – nilesh Feb 09 '15 at 18:24
  • First off, you should never trust content from w3schools.com - sometimes it's just rubbish. But in this case, read it again. It says those operators can be used **in** XPath expressions. The operands of `|` are themselves XPath expressions, but `|` is still part of the expression. In your line of code, it seems that `|` is _outside_ of any expression. – Mathias Müller Feb 09 '15 at 18:30
  • I not only read that, I also followed this SO question - http://stackoverflow.com/questions/12562597/two-conditions-using-or-in-xpath – nilesh Feb 09 '15 at 18:32
  • That's a question about XPath in Javascript - why do you expect the rules to be the same in Selenium? And you still do not understand me. I'm not saying that `|` is not an XPath operator (far from it). I am saying that `'//div' | '//span'`places `|` _outside_ of any XPath expression and I'm asking if this is allowed or understood by Selenium. – Mathias Müller Feb 09 '15 at 18:34
  • I understand your point, and I'm trying to say that if I read document correctly you could use `|` expression outside `[]`. For second point, As far as I know, JS uses browser Xpath engine and so does selenium-webdriver. only for IE webdriver injects xpath engine due to lack of it in IE. – nilesh Feb 09 '15 at 18:43
  • No, until now you haven't been trying to explain that this is allowed in the Selenium documentation. If so, why not link to this documentation? – Mathias Müller Feb 09 '15 at 18:44
  • 1
    Thanks. I don't understand the back tick (`). Does that mean evaluate what is inside? And is the if statement a java if or selenium if? – user3257891 Feb 09 '15 at 18:46
  • @MathiasMüller gotcha, I updated the answer, thanks. I added back tick because the code was looking like a comment without it and it has no significance as such. – nilesh Feb 09 '15 at 18:50
  • Sorry I forgot. You put backticks around code. I actually did that too and I think in my example it also showed up. – user3257891 Feb 09 '15 at 19:08