1

How to click on a specific text of search text box results in selenium WebDriver. In my case search text box is "School".

I'm sending keys "RGSchool1" in the text box "School" and then I want to click on "RGScool" when it shows up as results under the text box.

I tried all below approached by it's throwing "org.openqa.selenium.NoSuchElementException"

  1. Enter text and tab out
  2. Enter text and send enter key
  3. Absolute path - /html1/body1/div[7]/ul1/li1/div1/span1
  4. Relative path - //span[@class='select2-match']

HTML body:

<div class="select2-result-label" style="" xpath="1">
<span class="select2-match">RGSchoo</span>
l1 [rgschool1]</div>

Code: //Finding elements

@FindBy(id = "s2id_User_OrgId")
public WebElement clickJurisdiction;

@FindBy(xpath = "/html[1]/body[1]/div[6]/div[1]/input[1]")
public WebElement keyInJurisdiction;

@FindBy(xpath = "//div[@id='s2id_User_OrgUnitId']//a[@class='select2-        choice']")
public WebElement clickSchool;

@FindBy(xpath = "/html[1]/body[1]/div[7]/div[1]/input[1]")
public WebElement keyInSchool;

@FindBy(xpath = "/html[1]/body[1]/div[7]/ul[1]/li[1]/div[1]")
public WebElement schoolSearchResult2;

Call method:

public void enterNewUserData() {

    SeleniumTestHelper.enterText(firstName, Config.getProperty("FirstName"));
    SeleniumTestHelper.enterText(middleName, Config.getProperty("MiddleName"));
    SeleniumTestHelper.enterText(lastName, Config.getProperty("LastName"));
    SeleniumTestHelper.enterText(preferredName, Config.getProperty("PreferredName"));
    
    SeleniumTestHelper.clickOnButton(clickJurisdiction);
    SeleniumTestHelper.enterText(keyInJurisdiction, Config.getProperty("Jurisdiction"));
    SeleniumTestHelper.enter(keyInJurisdiction);

    SeleniumTestHelper.clickOnButton(clickSchool);
    SeleniumTestHelper.enterText(keyInSchool, Config.getProperty("School"));
    SeleniumTestHelper.clickOnButton(schoolSearchResult2); // It fails here

Please help me to find a solution. I'm new to this kind of scenario.

Please see the below-attached screenshot.

Screenshot before entering data:

Screenshot before entering data

Screenshot after entering data

marc_s
  • 675,133
  • 158
  • 1,253
  • 1,388
  • Can you post your full code ,then it will be more easier to answer – Haseeb Ahmed Aug 19 '20 at 05:04
  • Please read why a [screenshot of HTML or code or error is a bad idea](https://meta.stackoverflow.com/questions/303812/discourage-screenshots-of-code-and-or-errors). Consider updating the Question with formatted text based relevant HTML, code trials and error stack trace. – DebanjanB Aug 19 '20 at 05:20
  • @HaseebAhmed - Thanks for your time. Just modified my question with code details. – user13996330 Aug 19 '20 at 05:28
  • @DebanjanB - Thank you so much for your time in sharing that post. Yes, I'm a newbie here and figuring out how to add complete stack trace as StackOverflow is not allowing me to do so. Any suggestions on this? – user13996330 Aug 19 '20 at 05:43
  • Here i have answered a similar question. This might help you. https://stackoverflow.com/questions/63467976/how-can-i-select-a-search-suggestion-using-selenium-the-site-prevents-me-from-j/63469553#63469553 – rahul rai Aug 19 '20 at 06:29

1 Answers1

0

To locate the element with text as RGSchool1 you can use the following Locator Strategy:

  • Using cssSelector:

    @FindBy(cssSelector = "ul.select2-results div.select2-result-label > span.select2-match")
    public WebElement schoolSearchResult2;
    
  • Using xpath:

    @FindBy(xpath = "//ul[@class='select2-results']//div[@class='select2-result-label']/span[@class='select2-match']")
    public WebElement schoolSearchResult2;
    
DebanjanB
  • 118,661
  • 30
  • 168
  • 217
  • DebanjanB- Thanks a lot and appreciate your time. CSS selector which you provided worked with a wait of 2 seconds before clicking the element. :) – user13996330 Aug 19 '20 at 08:08
  • @user13996330 _...worked with a wait..._ I saw it coming, as you are using _Page Objects_. I don't suggest `sleeps` so _WebDriverWait_ was inevitable. But then the solution becomes way too complicated. – DebanjanB Aug 19 '20 at 08:21