我需要在上传图像时绕过 cloudflare 挑战,但我无法绕过它。我尝试了很多概念,但仍然无法解决问题。
当显示 cloudflare 时,我需要什么,它应该自动单击复选框,直到询问多少次,如果要求 25 次,它应该选中该框,直到下一个元素的下一次可见性:
我刚刚更新了代码,但仍然无法单击该字段
这里面临的问题是:
Traceback (most recent call last):
File "C:\Users\yazha\AppData\Roaming\JetBrains\PyCharmCE2023.1\scratches\scratch_6.py", line 14, in <module>
cf_element = wait.until(EC.presence_of_element_located((By.CLASS_NAME, "label.ctp-checkbox-label")))
File "D:\Python files\undetected-chromedriver-master\venv\lib\site-packages\selenium\webdriver\support\wait.py", line 95, in until
raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:
Cloudflare 挑战:
代码试验:
from selenium import webdriver
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
# Initialize webdriver
driver = webdriver.Chrome()
driver.maximize_window()
# Open Redbubble website and click on login
driver.get('redbubble url')
wait = WebDriverWait(driver, 20)
cf_element = wait.until(EC.presence_of_element_located((By.CLASS_NAME, "label.ctp-checkbox-label")))
num_attempts = 0
while True:
try:
cf_element.click()
num_attempts += 1
wait.until(EC.invisibility_of_element(cf_element))
cf_element = wait.until(EC.presence_of_element_located((By.CLASS_NAME, "select-image-single")))
except Exception as e:
print(f"Cloudflare captcha bypassed {num_attempts} times.")
break
# Continue with your code after bypassing the captcha
driver.get(the redbubble url)
time.sleep(20)
driver.find_element(By.ID, "select-image-single").click()
与文本关联的cloudflare启用的checkbox验证您是人类元素位于
<iframe>
内,因此您必须:
诱导 WebDriverWait 使所需的 框架可用并切换到它。
诱导 WebDriverWait 使所需的元素可点击。
您可以使用以下任一定位器策略:
使用CSS_SELECTOR:
driver.get("https://www.redbubble.com/portfolio/images/new")
time.sleep(5)
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[title='Widget containing a Cloudflare security challenge']")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "label.ctp-checkbox-label"))).click()
使用XPATH:
driver.get("https://www.redbubble.com/portfolio/images/new")
time.sleep(5)
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[@title='Widget containing a Cloudflare security challenge']")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//label[@class='ctp-checkbox-label']"))).click()
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
浏览器快照:
您可以在以下位置找到一些相关讨论:
这是用于绕过 Cloudflare 的完整 SeleniumBase 脚本。
pip install seleniumbase
,然后使用 python
运行:
from seleniumbase import SB
def verify_success(sb):
sb.assert_text("OH YEAH, you passed!", "h1", timeout=6.25)
sb.post_message("Selenium wasn't detected!", duration=2.8)
sb._print("\n Success! Website did not detect Selenium! ")
with SB(uc=True, incognito=True) as sb:
sb.open("https://nowsecure.nl/#relax")
try:
verify_success(sb)
except Exception:
sb.get_new_driver(undetectable=True, incognito=True)
sb.open("https://nowsecure.nl/#relax")
try:
verify_success(sb)
except Exception:
if sb.is_element_visible('iframe[src*="challenge"]'):
with sb.frame_switch('iframe[src*="challenge"]'):
sb.click("area")
else:
raise Exception("Detected!")
try:
verify_success(sb)
except Exception:
raise Exception("Detected!")
如有必要,它将单击复选框。使用
sb.driver
访问原始 driver
。