【问题标题】:selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted error clicking a radio-button using Selenium Pythonselenium.common.exceptions.ElementClickInterceptedException:消息:元素单击拦截错误单击使用 Selenium Python 的单选按钮
【发布时间】:2022-02-24 04:12:05
【问题描述】:

我正在尝试自动化以下网站 - https://apps.royalbank.com/apps/home-value-estimator#!/

使用以下代码可以正常工作 - 但是当我到达必须选择单选按钮的站点时,我无法单击此单选按钮:

 link = "https://apps.royalbank.com/apps/home-value-estimator#!/"
    WAIT = 1
    RUN = True
    driver = webdriver.Chrome (service=srv, options=options)    
    waitWebDriver = WebDriverWait (driver, 10)         
    print(f"\nChecking value for address: {address} on {link}...")
    if MINIMIZE:
      driver.minimize_window()    
    driver.get (link)    
    print(f"Input address...") 
    time.sleep(WAIT)       
    driver.find_element(By.XPATH, "//input[@name='address']").send_keys(address)    
    time.sleep(WAIT)           
    driver.find_element(By.XPATH, "//input[@name='address']").send_keys(Keys.ARROW_DOWN)    
    time.sleep(WAIT)           
    driver.find_element(By.XPATH, "//input[@name='address']").send_keys(Keys.ENTER) 
    time.sleep(WAIT)       
    driver.find_element(By.XPATH, "//form[@name='addressForm']//following-sibling::button").click()
    time.sleep(WAIT)     
    print(f"Select type...")
    if inpData[1] == "Detached House":
      driver.find_element(By.XPATH, "//label[@for='detached']").click()
    elif inpData[1] == "Condo Townhouse":
      driver.find_element(By.XPATH, "//label[@for='townhouse']").click()
    elif inpData[1] == "Semi Detached":
      driver.find_element(By.XPATH, "//label[@for='semidetached']").click()
    elif inpData[1] == "Condo Apt.":
      tmpElem = driver.find_element(By.XPATH, "//label[@for='condo']")
      if tmpElem.is_displayed():
        tmpElem.click()    
      else:
        print(f"Error - Address not found... - skipped...")
        RUN = False
    else:
      print(f"Error - Wrong Property Type {inpData[1]} in B2... - stopped...")
      RUN = False
    if RUN:    
      time.sleep(WAIT)       
      print(f"Select value and date...")
      driver.find_element(By.XPATH, "(//form[@name='typeForm']//following::button)[1]").click()
      time.sleep(WAIT)       
      driver.find_element(By.XPATH, "//input[@name='price']").send_keys(str(inpData[3]))    
      time.sleep(WAIT)       
      driver.find_element(By.XPATH, "//select[@name='month']").send_keys(str(inpData[4]))  
      time.sleep(WAIT)       
      driver.find_element(By.XPATH, "//select[@name='year']").send_keys(str(inpData[5]))    
      time.sleep(WAIT)
      driver.find_element(By.XPATH, "(//form[@name='priceForm']//following::button)[1]").click()
      time.sleep(WAIT)
      # driver.find_element(By.XPATH, "//input[@id='no-renovate']").click()      
      driver.find_element(By.XPATH, "//label[@for='no-renovate']").click()      
      time.sleep(WAIT)
      driver.find_element(By.XPATH, "(//form[@name='renovatingForm']//following::button)[1]").click()
      time.sleep(WAIT)
      print(f"Wait for value...")

它在这个语句中崩溃:

# driver.find_element(By.XPATH, "//input[@id='no-renovate']").click()      
driver.find_element(By.XPATH, "//label[@for='no-renovate']").click() 

带有错误信息:

File "C:\Users\Polzi\Documents\DEV\Fiverr\TRY\Collector85\checkAddrGS.py", line 231, in <module>
    driver.find_element(By.XPATH, "//label[@for='no-renovate']").click()
  File "C:\Users\Polzi\Documents\DEV\.venv\NormalScraping\lib\site-packages\selenium\webdriver\remote\webelement.py", line 80, in click
    self._execute(Command.CLICK_ELEMENT)
  File "C:\Users\Polzi\Documents\DEV\.venv\NormalScraping\lib\site-packages\selenium\webdriver\remote\webelement.py", line 693, in _execute
    return self._parent.execute(command, params)
  File "C:\Users\Polzi\Documents\DEV\.venv\NormalScraping\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 400, in execute
    self.error_handler.check_response(response)
  File "C:\Users\Polzi\Documents\DEV\.venv\NormalScraping\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 236, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element <label class="mob-mar-b-dbl font-18-important ng-binding" for="no-renovate">...</label> is not clickable at point (1148, 360). Other element would receive the click: <div id="loader" ng-show="loading" class="loader-overlay" tabindex="-1" aria-labelledby="loading-msg" role="alert" aria-live="assertive" style="">...</div>

html-sourcecode的部分如下所示:

<form name="renovatingForm" class="ng-valid ng-dirty ng-valid-parse" style="">
                        <fieldset class="radio-wpr pad-0 mob-mar-0 align-radio-btns mar-b-20">
                            <legend class="offscreen ng-binding"> Are You Planning on Doing Any Home Renovations?</legend>
                            <input type="radio" id="yes-renovate" name="plannedRenos" ng-model="data.hasPlannedRenos" value="1" class="ng-pristine ng-untouched ng-valid ng-not-empty" style="">
                            <label class="mob-mar-b-dbl font-18-important ng-binding" for="yes-renovate">Yes, I plan on renovating my home</label>
                            <input type="radio" id="no-renovate" name="plannedRenos" ng-model="data.hasPlannedRenos" value="0" class="ng-valid ng-not-empty ng-dirty ng-valid-parse ng-touched" style="">
                            <label class="mob-mar-b-dbl font-18-important ng-binding" for="no-renovate">No, I do not plan on renovating my home right now</label>
                        </fieldset>

                        <!-- ngIf: boughtAfter2005 --><p ng-bind-html="content.note_plan_on_renovating | trusted" class="mar-b-0 ng-binding ng-scope" ng-if="boughtAfter2005" style=""><span class="roboto-medium">Note:</span> If you’ve previously renovated your home, you will be able to add in those renovations later.</p><!-- end ngIf: boughtAfter2005 -->

                        <div class="controls-box desktop-controls-box">
                            <div class="go-back">
                                <a href="javascript:void(0);" data-dig-id="mortgages-sSX5mfsS7sZM-38" class="back-btn ng-binding" ng-click="setStep(2, data);">
                                    Back<span class="offscreen ng-binding">Click for previous Step</span>
                                </a>
                            </div>
                            <div>
                                <button href="javascript:void(0);" ng-show="data.hasPlannedRenos == 0" class="btn btn-primary ng-binding" ng-click="$root.GA4CustomEventTrigger('HVE - Planned Renovations - No Button'); setStep(5, data);" style="">
                                    Continue<span class="offscreen ng-binding">Next Step</span>
                                </button>
                                <button href="javascript:void(0);" ng-show="data.hasPlannedRenos == 1" class="btn btn-primary ng-binding ng-hide" ng-click="$root.GA4CustomEventTrigger('HVE - Planned Renovations - Yes Button'); setStep(4, data);">
                                    Continue<span class="offscreen ng-binding">Next Step</span>
                                </button>
                                <button href="javascript:void(0);" ng-show="data.hasPlannedRenos < 0" class="btn btn-primary btn-disabled ng-binding ng-hide" disabled="disabled">
                                    Continue<span class="offscreen ng-binding">Next Step</span>
                                </button>
                            </div>

                        </div>
                        <div class="controls-box mobile-controls-box">

                            <div>
                                <button href="javascript:void(0);" ng-show="data.hasPlannedRenos == 0" class="btn btn-primary ng-binding" ng-click="setStep(5, data);" style="">
                                    Continue<span class="offscreen ng-binding">Next Step</span>
                                </button>
                                <button href="javascript:void(0);" ng-show="data.hasPlannedRenos == 1" class="btn btn-primary ng-binding ng-hide" ng-click="setStep(4, data);">
                                    Continue<span class="offscreen ng-binding">Next Step</span>
                                </button>
                                <button href="javascript:void(0);" ng-show="data.hasPlannedRenos < 0" class="btn btn-primary btn-disabled ng-binding ng-hide" disabled="disabled">
                                    Continue<span class="offscreen ng-binding">Next Step</span>
                                </button>
                            </div>
                            <div class="go-back">
                                <a href="javascript:void(0);" data-dig-id="mortgages-sSX5mfsS7sZM-38" class="back-btn ng-binding" ng-click="setStep(2, data);">
                                    Back<span class="offscreen ng-binding">Click for previous Step</span>
                                </a>
                            </div>
                        </div>

                        <!--
    <a role="button" href="javascript:void(0);" ng-hide="showSanityError" class="btn-continue btn-circle fl-r mar-b-qtr price-btn" ng-click="setStep(3, data);"
       ng-class="{'fr' : lang == 'fr'}"><span class="offscreen">{{content.get_results}}</span></a>
    <a role="button" href="javascript:void(0);" ng-show="showSanityError" class="btn-continue btn-circle fl-r mar-b-qtr price-btn btn-disabled" ng-click=""
       ng-class="{'fr' : lang == 'fr'}"><span class="offscreen">{{content.get_results}}</span></a>
        -->

                        <div class="sanity-error acc-fw fl-l mobile-hide ng-binding ng-hide" ng-show="showSanityError">




                            The value entered appears to be outside the typical range for this neighbourhood (<span ng-show="lang == 'fr'" class="ng-hide">de </span> <span ng-show="lang == 'fr'" class="ng-hide">à</span><span ng-hide="lang == 'fr'">-</span> ), do you still want to continue? <a href="javascript:void(0);" ng-click="sendData(false);" class="ng-binding">Yes</a>
                        </div>


                    </form>

如何使用 Selenium 选择单选按钮?

【问题讨论】:

    标签: python selenium xpath css-selectors loader


    【解决方案1】:

    此错误消息...

    selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element <label class="mob-mar-b-dbl font-18-important ng-binding" for="no-renovate">...</label> is not clickable at point (1148, 360). Other element would receive the click: <div id="loader" ng-show="loading" class="loader-overlay" tabindex="-1" aria-labelledby="loading-msg" role="alert" aria-live="assertive" style="">...</div>
    

    ...暗示&lt;label&gt; 元素上的点击事件被loader-overlay 拦截。


    &lt;label&gt; 元素上的click()

    • 首先,您必须为加载器元素诱导WebDriverWaitinvisibility_of_element_located()

    • 然后将WebDriverWait诱导为所需的element_to_be_clickable(),您可以使用以下locator strategies之一:

      • CSS_SELECTOR

        WebDriverWait(driver, 20).until(EC.invisibility_of_element_located((By.CSS_SELECTOR, "div.loader-overlay#loader[ng-show='loading'][aria-labelledby='loading-msg']")))
        WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "label[for='no-renovate']"))).click()
        
      • XPATH

        WebDriverWait(driver, 20).until(EC.invisibility_of_element_located((By.XPATH, "//div[@class='loader-overlay' and @id='loader'][@ng-show='loading' and @aria-labelledby='loading-msg']")))
        WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//label[@for='no-renovate']"))).click()
        
    • 注意:您必须添加以下导入:

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

    【讨论】:

    • 我也不明白 - 当我检查网站并搜索“//div[@class='loader-overlay']”时 - 我找不到任何东西。这是为什么呢?
    • 感谢这很好用!我是否理解正确 - 隐形......我们仍然等待元素消失?您是如何找到使用长 xpath 选择的这个 div 元素的?
    • @Rapid1898 当我检查网站并搜索“//div[@class='loader-overlay']” - 我找不到任何东西:可能是element 是一个 javascript/AJAX 元素。检查 DOM 可以给我们一些线索。
    • @Rapid1898 long xpath:我只是想确保您正在等待确切的加载器消失,因为可能有多个加载器。如果加载程序可以唯一标识,您可以将 xpath 缩短为://div[@class='loader-overlay' and @id='loader'][@ng-show='loading']//div[@class='loader-overlay' and @id='loader']//div[@class='loader-overlay' and @id='loader'] 甚至//div[@class='loader-overlay']
    【解决方案2】:

    浏览我无法重现您的问题...我相信这可能是因为您正在使用 time.sleep 等待元素出现;这样做的缺点是有时等待太久(调试时很痛苦);或者有时不够长(并且您会出错并且必须通过测试尝试进行迭代)。

    更好的方法可能是在与元素交互之前利用要满足的预期条件。可能想试试下面的东西......

    from selenium.common import exceptions
    from selenium.common.exceptions import TimeoutException
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    
    
    
    def ClickByXPATH(NameOfObject):
        try:
            item = WebDriverWait(driver, 15).until(EC.presence_of_element_located((By.XPATH, NameOfObject)))
            item.click()
        except TimeoutException as e:
            print("Couldn't Click by name on: " + str(NameOfObject))
            pass
    #....
    #driver.find_element(By.XPATH, "//label[@for='no-renovate']").click()  
    ClickByXPATH("//label[@for='no-renovate']")
    #...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-04
      • 2021-05-16
      • 2021-10-14
      • 2016-01-19
      • 1970-01-01
      • 2019-03-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多