วิธีคลิกที่ผลลัพธ์ข้อความค้นหาในซีลีเนียม WebDriver

Aug 19 2020

วิธีคลิกที่ข้อความเฉพาะของกล่องข้อความค้นหาผลลัพธ์ในซีลีเนียม WebDriver ในกล่องข้อความค้นหาของฉันคือ "โรงเรียน"

ฉันกำลังส่งคีย์ "RGSchool1" ในกล่องข้อความ "โรงเรียน" จากนั้นฉันต้องการคลิกที่ "RGScool" เมื่อมันแสดงเป็นผลลัพธ์ใต้กล่องข้อความ

ฉันลองใช้วิธีการทั้งหมดด้านล่างนี้โดยการขว้างปา

  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คุณสามารถใช้Locator Strategyต่อไปนี้:

  • ใช้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;