1

I want to know what can I do to get the "Apple" text in span element with @FindBy annotation.

Thats html code:

<span class="ui-cell-data">Apple</span>

and I tried something like that:

@FindBy(className = "ui-cell-data:Samsung")
WebElement customerName;

but it didn't work!

  • 1
    What is the need to add ':Samsung' at the end of the classname value for findby? – Grasshopper Dec 18 '17 at 02:27
  • 1
    Additionally, `ui-cell-data` feels like *quite a broad class* and may potentially match other undesired elements as well. Though, you have not shown the markup of the page and it is impossible to tell at this point. – alecxe Dec 18 '17 at 02:48

3 Answers3

0

You can use @FindBys like a chained element look-up

@FindBys({@FindBy(className = "ui-cell-data")})
private WebElement element;

Or try using below:

@FindBy(xpath = "//*[@class = 'ui-cell-data']")
private WebElement element;

or

@FindBy(css = ".ui-cell-data")
private WebElement element; 

Hopefully it resolves your issue.

Ali Azam
  • 2,047
  • 1
  • 16
  • 25
0

You can try with xpath stated below

 @FindBy(xpath = "//span[@class = 'ui-cell-data']") 
 private WebElement element;
Mahmud Riad
  • 1,169
  • 1
  • 8
  • 19
0

As per the HTML you have shared you may/maynot have been able to get the Apple text within the span element with :

@FindBy(className = "ui-cell-data")
WebElement customerName;

Your code was nearly perfect but the trailing part in the className as :Samsung was unnecessary.

But again, looking at the class attribute it is expected that a couple of more <span> tags will have the same class. So to uniquely identify the intended WebElement we need to refer to a parent node and follow its decendents to reach this particular node.

Finally, with the given HTML the following code block will be much cleaner :

  • cssSelector:

    @FindBy(css = "span.ui-cell-data")
    WebElement customerName;
    
  • xpath:

    @FindBy(xpath = "//span[@class='ui-cell-data']")
    WebElement customerName;
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352