セレンWebDriverで検索テキストの結果をクリックする方法

Aug 19 2020

検索テキストボックスの特定のテキストをクリックする方法は、SeleniumWebDriverになります。私の場合、検索テキストボックスは「学校」です。

テキストボックス「School」でキー「RGSchool1」を送信していますが、テキストボックスの下に結果として表示されたら「RGScool」をクリックします。

「org.openqa.selenium.NoSuchElementException」をスローすることでアプローチした以下のすべてを試しました

  1. テキストを入力してタブアウト
  2. テキストを入力してEnterキーを送信します
  3. 絶対パス-/ html 1 / body 1 / div [7] / ul 1 / li 1 / div 1 / span 1
  4. 相対パス-// span [@ class = 'select2-match']

HTML本文:

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

コード://要素の検索

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

呼び出しメソッド:

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

解決策を見つけるのを手伝ってください。私はこの種のシナリオに不慣れです。

下記のスクリーンショットをご覧ください。

データを入力する前のスクリーンショット:

データ入力後のスクリーンショット

回答

DebanjanB Aug 19 2020 at 14:22

テキストがRGSchool1である要素を見つけるには、次のロケーター戦略を使用できます。

  • cssSelectorの使用:

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

    @FindBy(xpath = "//ul[@class='select2-results']//div[@class='select2-result-label']/span[@class='select2-match']")
    public WebElement schoolSearchResult2;