如何使用Selenium点击Cloudflare的“验证您是人类”复选框挑战。

huangapple go评论113阅读模式
英文:

How to click on "Verify you are human" checkbox challenge by cloudflare using Selenium

问题

抱歉,我只能为您提供代码的翻译,以下是您提供的Python代码的翻译:

  1. from selenium.webdriver.support.ui import WebDriverWait
  2. from selenium.webdriver.common.by import By
  3. from selenium.webdriver.support import expected_conditions as EC
  4. from selenium import webdriver
  5. import time
  6. chromedriver_path = r"./driver/chromedriver"
  7. browser = webdriver.Chrome(executable_path=chromedriver_path)
  8. url = "https://pace.coe.int/en/aplist/committees/9/commission-des-questions-politiques-et-de-la-democratie"
  9. topics_xpath = '//*[@id="challenge-stage"]/div/label/span[2]'
  10. browser.get(url)
  11. time.sleep(5) # 等待页面加载
  12. escolhe = browser.find_element("xpath", topics_xpath)
  13. time.sleep(5)
  14. escolhe.click()
  15. time.sleep(5)

希望这可以帮助您解决问题。如果您有其他问题或需要进一步的帮助,请告诉我。

英文:

I need to automatically download using Python a .csv file that is in this web page:

  1. https://pace.coe.int/en/aplist/committees/9/commission-des-questions-politiques-et-de-la-democratie

Now, I have written this code:

  1. from selenium.webdriver.support.ui import WebDriverWait
  2. from selenium.webdriver.common.by import By
  3. from selenium.webdriver.support import expected_conditions as EC
  4. from selenium import webdriver
  5. import time
  6. from selenium.webdriver.support import expected_conditions
  7. from selenium.webdriver.support import expected_conditions
  8. from selenium.webdriver.common.by import By
  9. from selenium.webdriver.support.ui import WebDriverWait
  10. chromedriver_path = r"./driver/chromedriver"
  11. browser = webdriver.Chrome(executable_path=chromedriver_path)
  12. url = "https://pace.coe.int/en/aplist/committees/9/commission-des-questions-politiques-et-de-la-democratie"
  13. topics_xpath = '//*[@id="challenge-stage"]/div/label/span[2]'
  14. browser.get(url)
  15. time.sleep(5) #Wait a little for page to load.
  16. escolhe = browser.find_element("xpath", topics_xpath)
  17. time.sleep(5)
  18. escolhe.click()
  19. time.sleep(5)

The web page opens up and I am then prompted to click on "Verify you are human":

如何使用Selenium点击Cloudflare的“验证您是人类”复选框挑战。

I have "inspected" the button and copied the xpath (see code above).
But I get this error:

  1. NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="challenge-stage"]/div/label/span[2]"}
  2. (Session info: chrome=114.0.5735.198)

Can anyone help me, please?

答案1

得分: 4

与文本“验证您是否为人类”元素相关联的[标签:复选框]位于<iframe>内,因此您需要:

  • 引发WebDriverWait以等待所需的_可用帧并切换到它_。
  • 引发WebDriverWait以等待所需的_可单击元素_。
  • 您可以使用以下任一定位策略之一:
    • 使用_CSS_SELECTOR_:
  1. driver.get("https://pace.coe.int/en/aplist/committees/9/commission-des-questions-politiques-et-de-la-democratie")
  2. time.sleep(5)
  3. WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[title='Widget containing a Cloudflare security challenge']")))
  4. WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "label.ctp-checkbox-label"))).click()
  • 使用_XPATH_:
  1. driver.get("https://pace.coe.int/en/aplist/committees/9/commission-des-questions-politiques-et-de-la-democratie")
  2. time.sleep(5)
  3. WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[@title='Widget containing a Cloudflare security challenge']")))
  4. WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//label[@class='ctp-checkbox-label']"))).click()
  • 注意:您需要添加以下导入语句:
  1. from selenium.webdriver.support.ui import WebDriverWait
  2. from selenium.webdriver.common.by import By
  3. from selenium.webdriver.support import expected_conditions as EC

参考

您可以在以下几个相关讨论中找到相关信息:

英文:

The [tag:checkbox] associated with the text Verify you are human element is within an <iframe> so you have to:

  • Induce WebDriverWait for the desired frame to be available and switch to it.

  • Induce WebDriverWait for the desired element to be clickable.

  • You can use either of the following locator strategies:

    • Using CSS_SELECTOR:

      1. driver.get("https://pace.coe.int/en/aplist/committees/9/commission-des-questions-politiques-et-de-la-democratie")
      2. time.sleep(5)
      3. WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[title='Widget containing a Cloudflare security challenge']")))
      4. WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "label.ctp-checkbox-label"))).click()
    • Using XPATH:

      1. driver.get("https://pace.coe.int/en/aplist/committees/9/commission-des-questions-politiques-et-de-la-democratie")
      2. time.sleep(5)
      3. WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[@title='Widget containing a Cloudflare security challenge']")))
      4. WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//label[@class='ctp-checkbox-label']"))).click()
  • Note : You have to add the following imports :

    1. from selenium.webdriver.support.ui import WebDriverWait
    2. from selenium.webdriver.common.by import By
    3. from selenium.webdriver.support import expected_conditions as EC

Reference

You can find a couple of relevant discussions in:

答案2

得分: 3

这是一个用于绕过Cloudflare的完整SeleniumBase脚本。

pip install seleniumbase,然后用python运行:

  1. from seleniumbase import SB
  2. def verify_success(sb):
  3. sb.assert_element('img[alt="Logo Assembly"]', timeout=8)
  4. sb.sleep(4)
  5. with SB(uc_cdp=True, guest_mode=True) as sb:
  6. sb.open("https://pace.coe.int/en/aplist/committees/9/commission-des-questions-politiques-et-de-la-democratie")
  7. try:
  8. verify_success(sb)
  9. except Exception:
  10. if sb.is_element_visible('input[value*="Verify"]'):
  11. sb.click('input[value*="Verify"]')
  12. elif sb.is_element_visible('iframe[title*="challenge"]'):
  13. sb.switch_to_frame('iframe[title*="challenge"]')
  14. sb.click("span.mark")
  15. else:
  16. raise Exception("Detected!")
  17. try:
  18. verify_success(sb)
  19. except Exception:
  20. raise Exception("Detected!")

只有在必要时才会点击复选框。使用sb.driver来访问原始的driver。该脚本尽量避免被检测到。

英文:

Here's a complete SeleniumBase script for bypassing Cloudflare on that site.

pip install seleniumbase, then run with python:

  1. from seleniumbase import SB
  2. def verify_success(sb):
  3. sb.assert_element('img[alt="Logo Assembly"]', timeout=8)
  4. sb.sleep(4)
  5. with SB(uc_cdp=True, guest_mode=True) as sb:
  6. sb.open("https://pace.coe.int/en/aplist/committees/9/commission-des-questions-politiques-et-de-la-democratie")
  7. try:
  8. verify_success(sb)
  9. except Exception:
  10. if sb.is_element_visible('input[value*="Verify"]'):
  11. sb.click('input[value*="Verify"]')
  12. elif sb.is_element_visible('iframe[title*="challenge"]'):
  13. sb.switch_to_frame('iframe[title*="challenge"]')
  14. sb.click("span.mark")
  15. else:
  16. raise Exception("Detected!")
  17. try:
  18. verify_success(sb)
  19. except Exception:
  20. raise Exception("Detected!")

It will only click the checkbox if necessary. Use sb.driver to access the raw driver. The script tries to avoid detection altogether.

huangapple
  • 本文由 发表于 2023年6月29日 01:00:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76575298.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定