Using multiple criteria to find a WebElement in Selenium

后端 未结 3 767
南方客
南方客 2021-01-02 02:59

I am using Selenium to test a website, does this work if I find and element by more than one criteria? for example :

 driverChrome.findElements(By.tagName(\"         


        
相关标签:
3条回答
  • 2021-01-02 03:38

    No it does not. You cannot concatenate/add selectors like that. This is not valid anyway. However, you can write the selectors such a way that will cover all the scenarios and use that with findElements()

    By byXpath = By.xpath("//input[(@id='id_Start') and (@class = 'blabla')]")
    List<WebElement> elements = driver.findElements(byXpath);
    

    This should return you a list of elements with input tags having class name blabla and having id id_Start

    0 讨论(0)
  • 2021-01-02 03:41

    CSS Selectors would be perfect in this scenario.

    Your example would

    By.css("input#id_start.blabla")
    

    There are lots of information if you search for CSS selectors. Also, when dealing with classes, CSS is easier than XPath because Xpath treats class as a literal string, where as CSS treats it as a space delimited collection

    0 讨论(0)
  • 2021-01-02 04:00

    To combine By statements, use ByChained:

    driverChrome.findElements(
        new ByChained(
            By.tagName("input"),
            By.id("id_Start"),
            By.className("blabla")
        )
    )
    

    However if the criteria refer to the same element, see @Saifur's answer.

    0 讨论(0)
提交回复
热议问题