我在找到正确的Xpath / ID输入字段时遇到问题,我以以下网站为例:
https://garden.lovetoknow.com/vegetable-garden/how-ripen-green-tomatoes-off-vine
打开链接然后向下滚动一点后,您会看到“写评论”蓝色按钮,单击它,然后填写文本和名称,它将出现一个reCaptcha example,我尝试了以下方法单击复选框,但未成功。如果有人可以帮助我,我将不胜感激
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, 10)
.until(ExpectedConditions.elementToBeClickable(By.cssSelector("div.recaptcha-checkbox-checkmark"))).click();
似乎您已经足够亲密。 <iframe>
的name看起来是动态的,因此最好避免使用[[name属性,并且可以使用以下Locator Strategies中的任何一个:使用
css_selector
:
new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("iframe[src^='https://www.google.com/recaptcha/api2/anchor']")));
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.cssSelector("span#recaptcha-anchor"))).click();
使用xpath
:
new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//iframe[starts-with(@src, 'https://www.google.com/recaptcha/api2/anchor')]")));
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.cssSelector("//span[@id='recaptcha-anchor']"))).click();
参考
您可以在以下位置找到几个相关的讨论: