Python Selenium元素点击拦截

标签 python selenium selenium-webdriver web-scraping xpath

我试图让 Selenium 单击一个链接,但它给出了一个错误,提示“点击被拦截”。

该代码确实运行了一次并加载了正确的网页,但从那以后我就没有让它运行了。 Selenium 确实找到了要单击的链接,但它不想使用它并返回错误。

错误代码:

Enter Anime:One Piece
Search All - MyAnimeList.net
Traceback (most recent call last):
  File "C:\Users\amete\Documents\Python\Code\Web test.py", line 29, in <module>
    link = driver.find_element_by_xpath("/html/body/div[2]/div[2]/div[3]/div[2]/div[2]/div[1]/div/article[1]/div[1]/div[2]/a[1]").click()
  File "C:\Users\amete\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\remote\webelement.py", line 80, in click
    self._execute(Command.CLICK_ELEMENT)
  File "C:\Users\amete\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\remote\webelement.py", line 633, in _execute
    return self._parent.execute(command, params)
  File "C:\Users\amete\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "C:\Users\amete\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element <a href="https://myanimelist.net/anime/21/One_Piece" class="hoverinfo_trigger fw-b fl-l" id="#revAreaAnimeHover21" rel="#revInfo21" style="position: relative;">...</a> is not clickable at point (103, 339). Other element would receive the click: <div height="576" class="qc-cmp-cleanslate css-1kaql54">...</div>
  (Session info: chrome=91.0.4472.114)

我的代码:

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

import time
Anime = input("Enter Anime:")

PATH = r"C:\Users\amete\Documents\chromedriver.exe"
driver = webdriver.Chrome(PATH)

driver.get("https://myanimelist.net/search/all?q=one%20piece&cat=all")
print(driver.title)

# Since Selenium does not want to allow me to use the main page to search
# I got a link that I already searched with and then cleared it.This then allowed me to type whatever I wanted.
search = driver.find_element_by_xpath('/html/body/div[2]/div[2]/div[3]/div[2]/div[1]/form/div/div[1]/input[1]')


# Clears the field
search.send_keys(Keys.CONTROL, 'a')
search.send_keys(Keys.DELETE)

# The field is now cleared and the program can type whatever it wants
search.send_keys(Anime)
search.send_keys(Keys.RETURN)

link = driver.find_element_by_xpath("/html/body/div[2]/div[2]/div[3]/div[2]/div[2]/div[1]/div/article[1]/div[1]/div[2]/a[1]").click()


time.sleep(5)

最佳答案

你需要:

1 等待字段变为可见

2 做一个稳定的定位器:

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

import time
# Anime = input("Enter Anime:")

driver = webdriver.Chrome(executable_path='/snap/bin/chromium.chromedriver')

driver.get("https://myanimelist.net/search/all?q=one%20piece&cat=all")
print(driver.title)

wait = WebDriverWait(driver, 20)
#  Accepting cookies
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, '#gdpr-modal-bottom button'))).click()
#  waiting for Search field
search = driver.find_element_by_xpath('//input[@name="q"]')
wait.until(EC.element_to_be_clickable((By.XPATH, '//input[@name="q"]')))

# Clears the field
search.send_keys(Keys.CONTROL, 'a')
search.send_keys(Keys.DELETE)

# The field is now cleared and the program can type whatever it wants
search.send_keys("anime")
search.send_keys(Keys.RETURN)
wait.until(EC.element_to_be_clickable((By.XPATH, '//h2[@id="anime"]//ancestor::div[@class="content-left"]//article[1]/div[contains(@class, "list")][1]/div[contains(@class, "information")]/a[1]')))
link = driver.find_element_by_xpath('//h2[@id="anime"]//ancestor::div[@class="content-left"]//article[1]/div[contains(@class, "list")][1]/div[contains(@class, "information")]/a[1]').click()


time.sleep(5)

代码在执行搜索后单击第一个元素。 如果需要单击其他元素,请将 XPath 部分更改为:div[contains(@class, 'list')][2], div[contains(@class, ' list')][3] 等等。您可以尝试缩短此 XPath(这可能是可能的)或找到另一个。

我建议看看 css 选择器。为您的情况找到它可能会更容易。 请注意,有一个 cookie 提示,在您的情况下不需要单击,但如果您尝试从页面底部获取元素,则可能需要单击。

更新

我更新了代码以包含单击“接受 cookie”按钮。

关于Python Selenium元素点击拦截,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68052382/

相关文章:

Selenium EdgeDriver 禁用保存密码对话框

java - 从 Jenkins 运行 JunitCore.runClasses

html - 元素在 Firefox 中不可点击

java - Selenium 从范围报告 3.1.5 迁移到 4.1.6

python - 在 python 日期时间中,为什么第一个月 'January' = 1,而一周的第一天 'Monday' = 0?

python - 有没有一种好方法可以将未知的 X 个列表压缩/合并在一起?

python - 无法选择值 - 错误 "Select only works on <select> elements, not on <a>"

python - 使用结构进行 django 部署 - TypeError : prepare_deployment() takes exactly 1 argument (0 given)

java - 如何在 C# Selenium WebDriver 中等待登录 cookie?

selenium - 如何将条件语句与 verifyElementPresent 一起使用