我试图让 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/