如何使用 Selenium 和 Java 点击 reCAPTCHA
How to click on the reCAPTCHA using Selenium and Java
为什么在尝试让驱动程序单击 reCAPTCHA 按钮时出现错误?
这是我试图让它工作的网站:https://rsps100.com/vote/760/
这是我目前的代码:
WebElement iframeSwitch = driver.findElement(By.xpath("/html/body/div[1]/div/div[1]/div/div/div[2]/div/form/div/div/div/div/iframe"));
driver.switchTo().frame(iframeSwitch);
driver.findElement(By.cssSelector("div[class=recaptcha-checkbox-checkmark]")).click();
如果有帮助,请使用 WebDriverWait
识别 element.See。
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//iframe[starts-with(@name,'a-')]")));
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("div.recaptcha-checkbox-checkmark")));
element.click();
这是应该有效的代码。
driver.switchTo().frame("a-9wt0e8vkopnm");
driver.findElement(By.xpath("//span[@id='recaptcha-anchor']")).click();
要在 reCaptcha checkbox 上调用 click()
,因为元素在 <iframe>
中,您需要:
- 为所需的 frameToBeAvailableAndSwitchToIt.
引入 WebDriverWait
- 为所需的 elementToBeClickable.
引入 WebDriverWait
您可以使用以下解决方案:
代码块:
public class ReCaptcha_click {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "C:\Utility\BrowserDrivers\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("start-maximized");
options.addArguments("disable-infobars");
options.addArguments("--disable-extensions");
WebDriver driver = new ChromeDriver(options);
driver.get("https://rsps100.com/vote/760");
new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//iframe[starts-with(@name, 'a-') and starts-with(@src, 'https://www.google.com/recaptcha')]")));
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("div.recaptcha-checkbox-checkmark"))).click();
}
}
浏览器快照:
这对我有用。请注意,我使用的是硒化物。对于普通的 selenium 代码看起来是一样的。
import static com.codeborne.selenide.Selenide.*;
void recaptchaTest() {
open("https://rsps100.com/vote/760");
switchTo().frame($x("//iframe[starts-with(@name, 'a-') and starts-with(@src, 'https://www.google.com/recaptcha')]"));
$("div.rc-anchor-content").click();
switchTo().defaultContent();
}
为什么在尝试让驱动程序单击 reCAPTCHA 按钮时出现错误?
这是我试图让它工作的网站:https://rsps100.com/vote/760/
这是我目前的代码:
WebElement iframeSwitch = driver.findElement(By.xpath("/html/body/div[1]/div/div[1]/div/div/div[2]/div/form/div/div/div/div/iframe"));
driver.switchTo().frame(iframeSwitch);
driver.findElement(By.cssSelector("div[class=recaptcha-checkbox-checkmark]")).click();
如果有帮助,请使用 WebDriverWait
识别 element.See。
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//iframe[starts-with(@name,'a-')]")));
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("div.recaptcha-checkbox-checkmark")));
element.click();
这是应该有效的代码。
driver.switchTo().frame("a-9wt0e8vkopnm");
driver.findElement(By.xpath("//span[@id='recaptcha-anchor']")).click();
要在 reCaptcha checkbox 上调用 click()
,因为元素在 <iframe>
中,您需要:
- 为所需的 frameToBeAvailableAndSwitchToIt. 引入 WebDriverWait
- 为所需的 elementToBeClickable. 引入 WebDriverWait
您可以使用以下解决方案:
代码块:
public class ReCaptcha_click { public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "C:\Utility\BrowserDrivers\chromedriver.exe"); ChromeOptions options = new ChromeOptions(); options.addArguments("start-maximized"); options.addArguments("disable-infobars"); options.addArguments("--disable-extensions"); WebDriver driver = new ChromeDriver(options); driver.get("https://rsps100.com/vote/760"); new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//iframe[starts-with(@name, 'a-') and starts-with(@src, 'https://www.google.com/recaptcha')]"))); new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("div.recaptcha-checkbox-checkmark"))).click(); } }
浏览器快照:
这对我有用。请注意,我使用的是硒化物。对于普通的 selenium 代码看起来是一样的。
import static com.codeborne.selenide.Selenide.*;
void recaptchaTest() {
open("https://rsps100.com/vote/760");
switchTo().frame($x("//iframe[starts-with(@name, 'a-') and starts-with(@src, 'https://www.google.com/recaptcha')]"));
$("div.rc-anchor-content").click();
switchTo().defaultContent();
}