Issue with CSS locator select-react

后端 未结 2 1014
没有蜡笔的小新
没有蜡笔的小新 2021-01-20 10:39

I have an issue with a CSS locator. I have a unique label for the parent, and from there I can get the child that I need.

@FindBy(css = \"[data-qa=\"select-S         


        
相关标签:
2条回答
  • 2021-01-20 11:25

    You had the right idea with your clearDropdown() method, there's just a more efficient way to get there.

    Seller would instead be defined as the parent, e.g.

    @FindBy(css = "[data-qa='select-Seller']")
    Webelement sellerParent; // probably needs a better name
    

    Then you would define a method for each element you want to either get or interact with that is based off of the parent element.

    public void clearDropdown(WebElement element)
    {
        element.findElement(By.cssSelector("[data-qa=icon-x]")).click();
    }
    
    // probably needs a better name
    public WebElement getChild(WebElement element)
    {
        return element.findElement(By.cssSelector(".select__value-container"));
    }
    

    Now you just call it like

    clearDropdown(sellerParent);
    

    or

    WebElement seller = getChild(sellerParent);
    

    for any parent element you want.

    0 讨论(0)
  • 2021-01-20 11:35

    You don't have to try to concatenate CSS selectors.

    It would be easier if you could define a parent as a WebElement:

    WebElement seller = driver.findElement(By.cssSelector("[data-qa=select-Seller]"));
    

    And then find elements inside it:

    WebElement sellerDropdown = seller.findElement(By.cssSelector(".select__value-container"));
    
    WebElement closeButton = seller.findElement(By.cssSelector("[data-qa=icon-x]"));
    

    Note how we are using seller.findElement instead of driver.findElement for child elements.


    I am not 100% sure how to describe this in FindBy terms, take a look if this helps:

    • Selenium/PageFactory: Find child elements using parent element's @FindBy?
    0 讨论(0)
提交回复
热议问题