How Selenium's ByChained class really works?

后端 未结 3 992
一生所求
一生所求 2020-12-30 08:10

I am very confused with what the documentation for ByChained class mentions. It says:

Mechanism used to locate elements within a docu

3条回答
  •  鱼传尺愫
    2020-12-30 08:35

    What this class does is allow you to locate an element using its heirarchy in the dom.

    lets say for some reason you have the following html:

    
        
            

    and you want to get the element that is between the div rather than the one on its own. You can use the ByChained by to specify that you want that element by doing the following:

    new ByChained(By.id("details"),By.id("firstName"));
    

    What happens is that it finds the first element then searches underneath that in the dom heirarchy for the selector that comes next in the list. Basically this By is just a nice clean way of no longer having to do the following:

    details = driver.findElement(By.id("details"));
    input = details.findElement(By.id("firstName"));
    

提交回复
热议问题