Locating child nodes of WebElements in selenium

前端 未结 5 2070
谎友^
谎友^ 2020-11-29 03:13

I am using selenium to test my web application and I can successfully find tags using By.xpath. However now and then I need to find child nodes within that node

5条回答
  •  甜味超标
    2020-11-29 03:27

    The toString() method of Selenium's By-Class produces something like "By.xpath: //XpathFoo"

    So you could take a substring starting at the colon with something like this:

    String selector = divA.toString().substring(s.indexOf(":") + 2);
    

    With this, you could find your element inside your other element with this:

    WebElement input = driver.findElement( By.xpath( selector + "//input" ) );
    

    Advantage: You have to search only once on the actual SUT, so it could give you a bonus in performance.

    Disadvantage: Ugly... if you want to search for the parent element with css selectory and use xpath for it's childs, you have to check for types before you concatenate... In this case, Slanec's solution (using findElement on a WebElement) is much better.

提交回复
热议问题