1

I have a text element on the page with properties:

Web element property

And I'm trying to get this element using FindBys annotation:

@FindBys({@FindBy(tagName = "h5"),@FindBy(id = "email_label")})
private WebElement EmailLabel;

but getting error: org.openqa.selenium.NoSuchElementException: Cannot locate an element using By.chained({By.tagName: h5,By.id: email_label})

At the same time element is found properly if use just one condition:

@FindBys({@FindBy(id = "email_label")})
private WebElement EmailLabel;

or

@FindBys({@FindBy(tagName = "h5")})
private WebElement EmailLabel;

I use:

<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
    <version>3.141.59</version>
</dependency>
<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-chrome-driver</artifactId>
    <version>3.141.59</version>
</dependency>
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
DmitriyH
  • 126
  • 7
  • I also had issues with this kind of stuff lately. Wasn't doing it with Annotations. In my case, I suspect somehow the server didn't recognise the user agent - or realised it was being accessed as a crawler... and it didn't serve up the content for specific dynamic content. I looked to see if the cookies etc would shed light on things too... and gave up. I also tried searching for it by accessing the whole page content as a text string. It never appeared - and introduced delays/retries. Never recognised the nodes. Very frustrating! – JGFMK Mar 20 '20 at 12:50
  • I also had worries that the xpath selenium web driver recognises is 'complete'. was doing things like (//select)[2] - and a myriad of variations to look at the node. Never found it. CSS Selectors/XPath Selectors. Nothing worked. – JGFMK Mar 20 '20 at 12:52

1 Answers1

1

FindBys Annotation

FindBys are used to mark a field on a to indicate that lookup should use a series of @FindBy tags in a chain as described in ByChained. As an example:

@FindBys({@FindBy(id = "foo"),
      @FindBy(className = "bar")})

Class ByChained

ByChained mechanism used to locate elements within a document using a series of other lookups. This class will find all DOM elements that matches each of the locators in sequence, Asan example:

driver.findElements(new ByChained(by1, by2))

The above line of code will find all elements that match by2 and appear under an element that matches by1. So essentially you shouldn't use both the by on the same node but one by for the parent node and the other for the child node and you can use the following Locator Strategy:

@FindBys({@FindBy(tagName = "h5"), @FindBy(tagName = "strong")})
private WebElement EmailLabel;

Ideally, to locate the <span> with text as What's your email address? you can use either of the following Locator Strategies:

  • Using css:

    @FindBys({@FindBy(css  = "h5#email_label>strong"), @FindBy(tagName = "span")})
    private WebElement EmailLabel;
    
  • Using xpath:

    @FindBys({@FindBy(xpath  = "//h5[@id='email_label']/strong"), @FindBy(xpath = "//span[contains(., 'your email address')]")})
    private WebElement EmailLabel;
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352