CSS定位器选择反应问题 [英] Issue with CSS locator select-react

查看:56
本文介绍了CSS定位器选择反应问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对CSS定位器有疑问。我为父母有一个唯一的标签,从那里我可以得到我需要的孩子。

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-Seller"] .select__value-container")
Webelement seller;
public Webelement getSeller(){ return seller; }

该类与所有下拉列表相似,因此唯一会改变的值是 data-qa 。另外,有时我需要取消选择它们。

The class is similar to all dropdowns, so the only value that will change is data-qa. Also, sometimes I need to deselect them.

为此,我有一个 X 定位器。

For that, I have an X locator.

@FindBy(css = "[data-qa="select-Seller"] [data-qa=icon-x]). 

如您所见,第一部分仍然相同。

As you can see, the first part is still the same.

所以我的问题是是否有可能编写某种方法(或任何其他方式)来更改定位器的最后一部分?我有600多个下拉列表并为 X 会让我发疯。

So my question is is it possible to write some method (or any other way) that will change the last part of the locator? I have 600+ dropdowns and creating 600+ new locators for an X will make me go nuts.

对我来说,做类似 element.click something的最好方法\sendkeys\ ... \ 使用默认部分(带有 .select__value-container ),但是如果我会写类似 element.deselect ,它将更改定位器,但我不知道如何。

For me, the best way to do something like element.click\sendkeys\...\ uses the default part (with .select__value-container), but if I will write something like element.deselect then it will change the locator, but I don't know how.

我写了这样的东西:

public void clearDropdown (WebElement element){
        String selector = element.toString();
        selector = selector.split(" ")[8];
        driver.findElement(By.cssSelector(selector + " [data-qa=icon-x]")).click();
    }

[[[ChromeDriver:MAC上的Chrome(99c7e4e38147c9f61da0c83c5ef1b992)]-> css选择器:[data-qa ='select-Seller'] .select__value-container] -这就是为什么 split()[8]

[[ChromeDriver: chrome on MAC (99c7e4e38147c9f61da0c83c5ef1b992)] -> css selector: [data-qa='select-Seller'] .select__value-container] - this is why "split(" ")[8]"

但是我认为这不是解决问题的正确方法。

But I don't think that this is the right way to solve the issue.

谢谢您的建议。

推荐答案

使用您的 clearDropdown()方法有一个正确的主意,这是一种更有效的方法。

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

卖方将改为父级,例如

@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);

WebElement seller = getChild(sellerParent);

您想要的任何父元素。

这篇关于CSS定位器选择反应问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆