且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

ElementClickInterceptedException:消息:元素单击拦截元素不可单击错误单击单选按钮使用 Selenium 和 Python

更新时间:2023-08-24 18:08:28

此错误信息...

selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: 
Element <input type="radio" name="docTypes" ng-model="$ctrl.documentTypes.selected" id="documentType-0" ng-change="$ctrl.onChangeDocumentType()" ng-value="documentType" tabindex="0" class="ng-pristine ng-untouched ng-valid ng-empty" value="[object Object]" aria-invalid="false"> 
is not clickable at point (338, 202). 
Other element would receive the click:
 <label translate-attr="{title: 'fulfillment.documentAction.createNew.modal.documentType.document.title'}" translate-values="{documentName: documentType.name}" for="documentType-0" translate="ASN - DSD" tabindex="0" title="Select ASN - DSD document type">...</label>

...暗示所需的元素不可点击,因为其他一些元素遮住了它.

...implies that the desired element wasn't clickable as some other element obscures it.

所需元素是 Angular 元素,以便在元素上调用 click()你必须为 element_to_be_clickable()WebDriverWait/code> 并且您可以使用以下任一定位器策略:

The desired element is a Angular element so to invoke click() on the element you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:

  • 使用CSS_SELECTOR:

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "label[for='documentType-0']"))).click()

  • 使用XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//label[@for='documentType-0']"))).click()
    

  • 注意:您必须添加以下导入:

  • Note : You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    

  • 作为替代,您可以使用 execute_script() 方法,如下所示:

    As an alternative you can use the execute_script() method as follows:

    • 使用CSS_SELECTOR:

    driver.execute_script("arguments[0].click();", WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "label[for='documentType-0']"))))
    

  • 使用XPATH:

    driver.execute_script("arguments[0].click();", WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//label[@for='documentType-0']"))))
    

  • 您可以在以下位置找到一些相关讨论:

    You can find a couple of relevant discussions in: