【问题标题】:Why getting this selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element为什么得到这个 selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element
【发布时间】:2022-12-04 18:59:32
【问题描述】:

我知道已经上传了对同一个问题的回答,但我试过它们对我不起作用,因为 selenium 代码也有一些更新。收到此错误selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element <div class="up-typeahead-fake" data-test="up-c-typeahead-input-fake">...</div> is not clickable at point (838, 0). Other element would receive the click: <div class="up-modal-header">...</div> ,尝试在此输入中发送我的搜索关键字时标有“技能搜索” 预先搜索弹出窗体。

这是网址:https://www.upwork.com/nx/jobs/search/modals/advanced-search?sort=recency&pageTitle=Advanced%20Search&_navType=modal&_modalInfo=%5B%7B%22navType%22%3A%22modal%22,%22title%22%3A%22Advanced%20Search%22,%22modalId%22%3A%221670133126002%22,%22channelName%22%3A%22advanced-search-modal%22%7D%5D

这是我的代码:

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.common.proxy import Proxy, ProxyType
import time
from fake_useragent import UserAgent
import pyttsx3
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

def main():
    options = Options()
    service = Service('F:\\work\\chromedriver_win32\\chromedriver.exe')
    options.add_argument("start-maximized")
    options.add_argument('--disable-blink-features=AutomationControlled') #Adding the argument
    options.add_experimental_option("excludeSwitches",["enable-automation"])#Disable chrome contrlled message (Exclude the collection of enable-automation switches)
    options.add_experimental_option('useAutomationExtension', False) #Turn-off useAutomationExtension
    options.add_experimental_option('useAutomationExtension', False) #Turn-off useAutomationExtension
    prefs = {"credentials_enable_service": False,
     "profile.password_manager_enabled": False}
    options.add_experimental_option("prefs", prefs)
    ua = UserAgent()
    userAgent = ua.random
    options.add_argument(f'user-agent={userAgent}')
    driver = webdriver.Chrome(service=service , options=options)
    url = 'https://www.upwork.com/nx/jobs/search/?sort=recency'
    driver.get(url)
    time.sleep(7)



    advsearch = driver.find_element(By.XPATH,'//button[contains(@title,"Advanced Search")]')
    advsearch.click()
    time.sleep(10) 
    skill = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH,'//div[contains(@class,"up-typeahead")]')))
    skill.click()
    time.sleep(10)
    keys = ["Web Scraping","Selenium WebDriver", "Data Scraping", "selenium", "Web Crawling", "Beautiful Soup", "Scrapy", "Data Extraction", "Automation"]
    for i in range(len(keys)):

        skill.send_keys(Keys[i],Keys.ENTER)
        time.sleep (2)

main()

我尝试将键发送到输入字段,但它给我 Error .ElementClickInterceptedException ,我尝试从堆栈上一个与此错误相关的问题答案中获取旧答案,但它们对我不起作用,因为 selenium 代码也有一些更新。

【问题讨论】:

    标签: python selenium selenium-webdriver xpath automation


    【解决方案1】:

    该错误表明您必须单击使用 JS 执行,如:

     import time
    
     skill = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH,'//div[contains(@class,"up-typeahead")]')))
     driver.execute_script("arguments[0].click();" ,skill)
     time.sleep(1)
    

    【讨论】:

    • name 'jsvascript_execute' is not defined 我在用我的代码替换你的代码时收到此错误
    • @Info Rewind,我已经编辑了
    • 谢谢,但它仍然没有得到钥匙,这给了我这个错误 skill.send_keys(Keys[i],Keys.ENTER) TypeError: 'type' object is not subscriptable
    • 试试 skill.send_keys(Keys[i].ENTER)。其实是另外一个问题。如果它不起作用那么最好提出一个新问题。谢谢
    • @Fazlul 你的回答是错误的。看我的回答...
    【解决方案2】:

    通过单击“高级搜索”按钮,将打开一个高级搜索模式对话框。因此,当此对话框打开时,您无法将搜索输入插入常规搜索输入,只能插入该模式对话框输入。然后您需要关闭该对话框上的按钮以执行搜索。
    以下代码正在运行:

    import time
    
    from selenium import webdriver
    from selenium.webdriver.chrome.service import Service
    from selenium.webdriver.chrome.options import Options
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
    options = Options()
    options.add_argument("start-maximized")
    
    webdriver_service = Service('C:webdriverschromedriver.exe')
    driver = webdriver.Chrome(options=options, service=webdriver_service)
    wait = WebDriverWait(driver, 10)
    
    url = "https://www.upwork.com/nx/jobs/search/?sort=recency"
    driver.get(url)
    
    keys = ["Web Scraping","Selenium WebDriver", "Data Scraping", "selenium", "Web Crawling", "Beautiful Soup", "Scrapy", "Data Extraction", "Automation"]
    wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'button#onetrust-accept-btn-handler')))
    time.sleep(5)
    wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'button#onetrust-accept-btn-handler'))).click()
    for i in range(len(keys)):
        wait.until(EC.element_to_be_clickable((By.XPATH, '//button[contains(@title,"Advanced Search")]'))).click()
        advanced_search_input = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,'[data-test="modal-advanced-search-and_terms"]')))
        advanced_search_input.clear()
        advanced_search_input.send_keys(keys[i])
        wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,'[data-test="modal-advanced-search-search-btn"]'))).click()
    

    此外,在使用 Selenium 时,您应该绝不使用 JavaScript 点击,直到你别无选择,因为 Selenium 模仿人类 GUI 操作,而 JavaScript 点击可以对不可见、覆盖的元素等执行点击。
    在这种情况下,当对话框以用户身份打开时,您不能单击该对话框所涵盖的元素。因此,当使用 Selenium 执行 GUI 测试时(这就是 Selenium 的用途),您不应该使用 JavaScript 对此类元素执行强制单击。

    【讨论】:

      猜你喜欢
      • 2016-09-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-01
      • 2021-11-09
      • 2021-12-31
      • 1970-01-01
      相关资源
      最近更新 更多