3

Is it possible to combine/concat two Bys?

If I have one By

By parentBy = new By.xpath(".//div[@class='parent']")

and another By

By childBy = new By.xpath(".//div[@class='child']")

is it possible to concat the two Bys to a new one that has this xpath?

By combinedBy = new By.xpath(".//div[@class='parent']/div[@class='child']")

Something like

By combinedBy1 = parentBy + childBy
By combinedBy2 = parentBy.Concat(childBy)

Usecase:

We use the page object model.

Now I have a table as kind of child page object model. This table should have a method to select some data. Because of some html-structure issues (it is third party) I have to xpath a cell of the table that is child of a div (the row) by checking for a class of the cell and the text/content of this cell.

Tobias
  • 2,945
  • 5
  • 41
  • 59
  • 3
    As for me, it doesn't make any sense because you can write any complex xpath without combining `By`. Can you give an example where you want to apply your combined `By`? – olyv Jun 13 '14 at 13:03
  • whats the need of it?In which case you need to do so.Plz elaborate as may be there is some straight forward simple solution. – Abhishek_Mishra Jun 13 '14 at 13:04
  • 1
    If your expressions are too long, you might prefer to save it in variables and concatenate the strings and "/" to combine them. – helderdarocha Jun 13 '14 at 14:01

2 Answers2

11

In addition to what @SiKing said, I would also recommend looking at the ByChained class (see here for javadocs).

Example on how to use it:

driver.findElements(new ByChained(By.id("product-table"), By.className("quantity")));

Not sure if this addresses OP's question directly, but still very useful.

Priidu Neemre
  • 2,813
  • 2
  • 39
  • 40
5

You have two options:

  1. You can do something like:

    WebElement parentEl = driver.findElement(By.xpath(""))
    WebElement childEl = parentEl.findElement(By.className(""))
    
  2. Use the PageFactory @FindBys. See Selenium PageFactory and Selenium API.

SiKing
  • 10,003
  • 10
  • 39
  • 90