代码之家  ›  专栏  ›  技术社区  ›  Pratik Singh

无法通过硒元素定位元素

  •  0
  • Pratik Singh  · 技术社区  · 2 年前

    我正在尝试使用Selenium定位疫苗接种日期元素,以便可以根据我的要求传递输入,但无法使用By.ID或By.XPATH进行传递。

    id = 'mat-input-2'
    XPATH = '//\*\[@id="mat-input-2"\]'
    

    密码

    vaccination_date = driver.find_element(By.XPATH, '//*[@id="mat-input-2"]').click()
    

    HTML:

    <div _ngcontent-lhj-c245="" class="col-md-4">
        <label _ngcontent-lhj-c245="" for="vaccinationDate" class="form-label">Vaccination Date<span _ngcontent-lhj-c245="" class="mandatory-field">*</span></label>
        <input _ngcontent-lhj-c245="" matinput="" formcontrolname="vaccinationDate" placeholder="  DD/MM/YYYY" class="mat-input-element mat-form-field-autofill-control mat-datepicker-input form-control vaccDate ng-pristine cdk-text-field-autofill-monitored ng-valid ng-touched" id="mat-input-2" data-placeholder="  DD/MM/YYYY" aria-invalid="false" aria-required="false" aria-haspopup="dialog" max="2024-04-21T00:00:00+05:30" data-mat-calendar="mat-datepicker-2" min="2024-02-17T05:30:00+05:30">
        <mat-datepicker-toggle _ngcontent-lhj-c245="" matsuffix="" class="mat-datepicker-toggle picker" data-mat-calendar="mat-datepicker-2">
            <button mat-icon-button="" type="button" class="mat-focus-indicator mat-icon-button mat-button-base" aria-haspopup="dialog" aria-label="Open calendar" tabindex="0">
                <span class="mat-button-wrapper">
                    <svg viewBox="0 0 24 24" width="24px" height="24px" fill="currentColor" focusable="false" class="mat-datepicker-toggle-default-icon ng-star-inserted">
                        <path d="M19 3h-1V1h-2v2H8V1H6v2H5c-1.11 0-1.99.9-1.99 2L3 19c0 1.1.89 2 2 2h14c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2zm0 16H5V8h14v11zM7 10h5v5H7z"></path>
                    </svg>
                    <!---->
                </span>
                <span matripple="" class="mat-ripple mat-button-ripple mat-button-ripple-round"></span>
                <span class="mat-button-focus-overlay"></span>
            </button>
        </mat-datepicker-toggle>
        <mat-datepicker _ngcontent-lhj-c245=""></mat-datepicker>
        <!---->
        <span _ngcontent-lhj-c245="" class="alert-message ng-star-inserted">       <!----><!----><!----><!---->    </span>
        <!---->
    </div>
    

    我试过这样做:

    vaccination_date = driver.find_element(By.ID, 'mat-input-2').click()
    vaccination_date = driver.find_element(By.XPATH, '//*[@id="mat-input-2"]').click()
    
    1 回复  |  直到 2 年前
        1
  •  -1
  •   Ketan Vekariya    2 年前

    看起来您正试图使用ID和XPath来定位疫苗接种日期输入元素,但遇到了困难。让我们尝试几个故障排除步骤:

    1. 等待元素可点击: 有时,由于页面加载或其他因素,元素可能无法立即点击。请尝试等待元素可单击,然后再单击它。
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    
    vaccination_date = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, 'mat-input-2')))
    vaccination_date.click()
    
    1. 检查元素是否在Iframe内: 如果元素在iframe中,则需要在与该元素交互之前切换到该iframe。

    2. 检查动态ID或类: 有时,ID或类可能是动态生成的。在这种情况下,请尝试使用其他更稳定的属性来定位元素。

    vaccination_date = driver.find_element(By.XPATH, '//input[@formcontrolname="vaccinationDate"]')
    vaccination_date.click()
    

    试试这些步骤,如果你遇到任何问题,请告诉我!