6

I want to select all the tags with <td class='blob-code blob-code-addition'> and <td class='blob-code blob-code-deletion'> . So I am trying to include or condition here between the two predicates. It does not work. However, if I include only one of the two classes it works . What is the problem here? Something is wrong with the syntax.

By getChanges = By.xpath("//td[@class='blob-code blob-code-addition'] or  //td[@class='blob-code blob-code-deletion']");
Zack
  • 2,078
  • 10
  • 33
  • 58

5 Answers5

8

You want to specify that like the following:

//td[contains(@class,'deletion') or contains(@class,'addition')]

or

//td[contains(@class,'blob-code blob-code-addition') or contains(@class,'blob-code blob-code-deletion')]

If you want to do a tag independent search then you can simply use

//*[contains(@class,'blob-code blob-code-addition') or contains(@class,'blob-code blob-code-deletion')]

From your answer it looks like you are trying to concatenate two different xpaths

However, contians() is not mandatory here. You also can do without this

//*[(@class='blob-code blob-code-addition') or (@class='blob-code blob-code-deletion')]
Saifur
  • 16,081
  • 6
  • 49
  • 73
  • So we cannot use "or" directly? It has to go with contains? – Zack Feb 15 '15 at 18:44
  • @Zack you can but not starting with `//`. I am using `or` as well. Please see my edit. – Saifur Feb 15 '15 at 18:45
  • Yes. But in all the three solutions you have used "contains". It worked. I was just curious why it did not work without contains. – Zack Feb 15 '15 at 18:46
3

what works for me is below expression using "|" character inside my expression-

By element = driver.findElement(By.xpath("//button[@clas='xyz'] | //button[@clas='abc']"))

I used above expression for JAVA + Selenium + Maven project

Mike ASP
  • 2,013
  • 2
  • 17
  • 24
0

using pom: @FindBy(xpath ="//span[contains(@class,'vui-menuitem-label-text') and normalize-space(.) = 'Clone']")

Michał Turczyn
  • 32,028
  • 14
  • 47
  • 69
Anil Jain
  • 57
  • 3
0

To select all the tags with:

  • <td class="blob-code blob-code-addition">
  • <td class="blob-code blob-code-deletion">

You can use either of the following Locator Strategies:

  • Using xpath through class attribute with or clause:

    //td[@class='blob-code blob-code-addition' or @class='blob-code blob-code-deletion']
    
  • Using xpath through single class attribute with or clause:

    //td[contains(@class,'blob-code-addition') or contains(@class,'blob-code-deletion')]
    
  • Using xpath through partial class attribute with or clause:

    //td[contains(@class,'addition') or contains(@class,'deletion')]
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0

For writing Multiple Xpath in one element selenium

driver.find_elements(By.XPATH, '//div[@class="any-content"]//p|//h3|//figure')

You can write many tags by using "|" modulo.