我目前正在努力绕过 2captcha API 演示页面上的旋转验证码。我遇到的问题与集成方面无关;我已经弄清楚如何将它与 2captcha API 集成。然而,挑战在于使用 JavaScript 将输出(在我们的例子中为“165 度”)正确插入到适当的位置。
经过仔细分析,我发现每次右键单击验证码图像都会将其旋转 15 度,并相应地更新“img”HTML 标记的“style”属性。因此,通过点击右键11次,我们就达到了想要的165度旋转,成功解决了验证码。
虽然我的脚本有效地添加了旋转,确保图像显示正确旋转,但在单击“检查”按钮时遇到失败。以下是 HTML 片段:
<div class="_3i4YsKXQ1sT9uqskDtNNuA"><img src="/dist/web/43.png" alt="rotatecaptcha example" width="118" height="118" style="transform: rotate(165deg);"></div>
我尝试了很多搜索,但找不到任何关于这种验证码的博客或教程,堆栈上还有一些其他相关问题,但这些都是简单的验证码,就像这样one。
这是可重现的代码:
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from time import sleep
import requests
# Initialize Chrome webdriver
option = webdriver.ChromeOptions()
# option.add_argument("--headless")
option.add_argument("--start-maximized")
driver = webdriver.Chrome(ChromeDriverManager().install(), options=option)
# Load the webpage
driver.get('https://2captcha.com/demo/rotatecaptcha')
image_element = driver.find_element(By.XPATH, '//img[@alt="rotatecaptcha example"]')
new_style = f"transform: rotate(165deg);"
driver.execute_script(f"arguments[0].setAttribute('style', '{new_style}')", image_element)
sleep(3)
try:
click_element = driver.find_element(By.XPATH, '//button[@type="submit"]').click(); sleep(15)
except:
print('Not able to locate the click button')
您无法通过更改
style
来解决它,因为它会检查 JavaScript 中的某些变量,当用户按下带有箭头的按钮时,这些变量会发生更改。
您必须单击此箭头
arrow = driver.find_element(By.XPATH, '//button[contains(@class, "rotateRight")]')
for _ in range(11):
arrow.click()
完整的工作代码 - 使用
Firefox
进行测试
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
#from webdriver_manager.chrome import ChromeDriverManager
from webdriver_manager.firefox import GeckoDriverManager
from selenium.webdriver.common.by import By
from time import sleep
#option = webdriver.ChromeOptions()
option = webdriver.FirefoxOptions()
# option.add_argument("--headless")
option.add_argument("--start-maximized")
#driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=option)
driver = webdriver.Firefox(service=Service(GeckoDriverManager().install()), options=option)
driver.get('https://2captcha.com/demo/rotatecaptcha')
sleep(1)
arrow = driver.find_element(By.XPATH, '//button[contains(@class, "rotateRight")]')
for _ in range(11):
arrow.click()
try:
button = driver.find_element(By.XPATH, '//button[@type="submit"]')
button.click()
sleep(15)
#input('Press ENTER to close')
except:
print('Not able to locate the click button')