这是我的代码。
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
from selenium.webdriver.chrome.options import Options
from selenium import webdriver
from bs4 import BeautifulSoup as bs
import re as re
import time
import os
import sys
url = 'https://www.ufc.com/athletes/all'
driver = webdriver.Chrome('/Applications/Python 3.9/chromedriver')
driver.get(url)
driver.implicitly_wait(5)
driver.find_element_by_xpath('//*[@id="onetrust-close-btn-container"]/a').click()
loadingButton = WebDriverWait(driver,10).until(EC.presence_of_element_located((By.PARTIAL_LINK_TEXT, "LOAD MORE")))
maxTires = 200;
while loadingButton:
loadingButton.click()
time.sleep(3)
WebDriverWait(driver,30).until(EC.presence_of_element_located((By.XPATH,"/html/body/div[1]/div/main/div[1]/div/div/div/div/div/div/div/div/div[2]/div/div/ul/li/a")))
loadElems = driver.find_elements_by_xpath('//*[@id="block-mainpagecontent"]/div/div/div[2]/div/div/ul/li/a')
if len(loadElems)>0:
loadingButton = driver.find_element_by_xpath('//*[@id="block-mainpagecontent"]/div/div/div[2]/div/div/ul/li/a')
tiresLoaded = len(driver.find_elements_by_css_selector("#block-mainpagecontent > div > div > div.views-element-container.block.block-views.block-views-blockall-athletes-page > div > div > ul > li > a"))
else:
print("Loaded all the tires")
break
if tiresLoaded >= maxTires:
print (tiresLoaded + " are loaded successfully.")
break
my_hrefs = [my_elem.get_attribute("href") for my_elem in WebDriverWait(driver, 90).until(EC.visibility_of_all_elements_located((By.XPATH, "/html/body/div[1]/div/main/div[1]/div/div/div/div/div/div/div/div/div[2]/div/div/div/div[1]/div/div[1]/ul/li[1]/div/div/div/div[2]/div[3]/a/span")))]
windows_before = driver.current_window_handle
for my_href in my_hrefs:
driver.execute_script("window.open('" + my_href +"');")
WebDriverWait(driver, 10).until(EC.number_of_windows_to_be(2))
windows_after = driver.window_handles
new_window = [x for x in windows_after if x != windows_before][0]
driver.switch_to.window(new_window)
time.sleep(3)
print(link.text)
nickname = driver.find_element_by_xpath('/html/body/div[1]/div/main/div[1]/div/div/div/div[1]/div[1]/div[3]/div[1]/div[1]/div[1]/div').text
fighter_name = driver.find_element_by_xpath('/html/body/div[1]/div/main/div[1]/div/div/div/div[1]/div[1]/div[3]/div[1]/div[1]/div[2]/h1').text
status = driver.find_element_by_xpath('/html/body/div[1]/div/main/div[1]/div/div/div/div[2]/div[8]/div/div/div/div[3]/div[1]/div[1]/div/div[2]')
Rank_Division_Record = driver.find_element_by_xpath('/html/body/div[1]/div/main/div[1]/div/div/div/div[1]/div[1]/div[3]/div[1]/div[1]/div[3]/text()')
age = driver.find_element_by_xpath('/html/body/div[1]/div/main/div[1]/div/div/div/div[2]/div[8]/div/div/div/div[3]/div[1]/div[3]/div[1]/div[2]/div')
leg_reach = driver.find_element_by_xpath('/html/body/div[1]/div/main/div[1]/div/div/div/div[2]/div[8]/div/div/div/div[3]/div[1]/div[4]/div[3]/div[2]')
driver.close()
driver.switch_to.window(windows_before)
driver.quit()
我不断收到错误
selenium.common.exceptions.ElementClickInterceptedException: Message: element Click Intercepted: Element ...在点 (600, 685) 处不可单击。其他元素将收到点击:... (会话信息:chrome=91.0.4472.164)
我在单击之前不断增加等待的时间,但此错误仍然出现。我很欣赏任何人能给出的任何见解。
我可以看到加载屏幕时显示cookie弹出窗口,因此首先使用explicitWait
处理cookie
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//button[text()='Accept Cookies']"))).click()
然后,您可以使用下面的explicitWait
单击"加载更多"按钮
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//a[text()='Load More']"))).click()
更新
您可以在最大化浏览器模式下执行脚本吗driver.maximize_window()
当我执行脚本时,它运行良好,直到最后一页Load More
按钮不可用,它会抛出TimeoutException
也尝试在incognito
模式下执行脚本以避免缓存问题
所以为了证明我的观点,只需检查下面的图片
https://i.stack.imgur.com/5Ifxi.pnghttps://i.stack.imgur.com/MPGdg.png
看起来该元素与另一个元素重叠,因此 click() 方法不起作用。实际上它正在单击,但单击是在重叠的元素上完成的。
你能试试吗 driver.find_element_by_xpath().send_keys(键。回车)