3

I am currently learning page object model (POM) and I am trying to access a specific web element using @FindBy but I am not sure how to correctly write the syntax for my element into @FindBy?

What I have is:

driver.findElement(By.cssSelector("a[dta-qid='inventory']");

So my question is how do I place a[da-qid='inventory'] correctly into @FindBy?


By, a[da-qid='inventory'], what I mean is that it selects every <a> element whose da-qid value begins with 'inventory'.

Robben
  • 457
  • 2
  • 8
  • 20

3 Answers3

2

Why do not you read through this? Use of @FindsBy is easier if you do that with How Enum. You have multiple options in that case. With cssSelector it should look like this

@FindBy(how = How.css, using = "a[dta-qid='inventory']") 
WebElement foobar;
Saifur
  • 16,081
  • 6
  • 49
  • 73
  • there is actually no need to use the enum explicitly. you can directly declare the kind of selector in the @FindBy annotation. – Carsten Jul 24 '15 at 01:44
  • 1
    @Carsten sure. That's why I said "easier". And, that's one of features commonly used and readability also matters – Saifur Jul 24 '15 at 01:51
  • Okay, so it comes down to personal taste of what is easier readable ;) – Carsten Jul 24 '15 at 01:53
  • @Carsten Is that a question? – Saifur Jul 24 '15 at 02:06
  • No, it's not a question. I personally find the "how" parameter to be decreasing the readability, since it is cluttering the annotation body. And since we are disagreeing on that one, it is obviously a matter of taste. – Carsten Jul 24 '15 at 02:09
1

If you assume that multiple elements will be found using this selector, try the following:

@FindBy(css="a[da-qid='inventory']")
List<WebElement> elements;

Just don't forget to choose correctly between da-qid='inventory' and dta-qid='inventory'

TEH EMPRAH
  • 1,828
  • 16
  • 32
0

You could use the XPath selector:

driver.findElement(By.xpath("//a[contains(@da-qid,'inventory')]");

or

@FindBy(xpath = "//a[contains(@da-qid,'inventory')]")
WebElement inventoryLink;

respectively

@FindAll(xpath = "//a[contains(@da-qid,'inventory')]")
List<WebElement> inventoryLinks;

Theoretically the XPath "//a[startsWith(@da-qid,'inventory')]" exists as well, but it didn't work in all WebDrivers for me.

Carsten
  • 2,047
  • 1
  • 21
  • 46