Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium: Change By-instance or concat two By-instances

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.

like image 874
Tobias Avatar asked Oct 12 '25 03:10

Tobias


2 Answers

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.

like image 82
Priidu Neemre Avatar answered Oct 14 '25 17:10

Priidu Neemre


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.

like image 21
SiKing Avatar answered Oct 14 '25 18:10

SiKing