[英]How to detect captcha on a web page using selenium c#?
我使用 selenium c# 创建了一个有趣的小机器人,它在一个名为 omegle.com 的网站上玩井字游戏,但它总是被 captcha 阻止
那么如何检查页面上是否存在验证码?
(注意:我没有说旁路)
我试过By.XPath("...")
和By.ClassName("...")
但它会在一段时间后停止工作(可能是因为 XPath 发生了变化)
这是我到目前为止所做的:
public int captcha() //detects captcha , 1= solved
{
try //checks if captcha is on page and asks user to solve it
{
IWebElement temp=driver.FindElement(By.______("..."));
Console.WriteLine(temp.Text);
}
catch { goto end; } //if captcha is not on the page
Console.WriteLine("CAPTCHA DETECTED! Solve captcha to proceed!");
while(true)
{
try //keeps checking if captcha is still on page
{
driver.FindElement(By.____("..."));
}
catch { break; } //breaks out of loop if captcha is solved
}
Console.WriteLine("Captcha Solved!");
end:;
return 1;
}
这里还有 iframe 标签:
<iframe src="https://www.google.com/recaptcha/api2/anchor?ar=1&k=6LekMVAUAAAAAPDp1Cn7YMzjZynSb9csmX5V4a9P&co=aHR0cHM6Ly93d3cub21lZ2xlLmNvbTo0NDM.&hl=en&v=UFwvoDBMjc8LiYc1DKXiAomK&size=normal&cb=t8ss8cm29p9u" role="presentation" name="a-f9qa6z183usf" scrolling="no" sandbox="allow-forms allow-popups allow-same-origin allow-scripts allow-top-navigation allow-modals allow-popups-to-escape-sandbox allow-storage-access-by-user-activation" width="304" height="78" frameborder="0"></iframe>
xpath 应该是: //iframe[contains(@src,'recaptcha')]
然后,您可以通过使用 try/catch 块(在元素未找到异常上)或使用 FindElements 获取所有元素并查看大小是否非零来检查元素是否存在:
Boolean isPresent = driver.FindElements(By.XPath("//iframe[contains(@src,'recaptcha')]")).size() > 0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.