Selenium,webdriver - 找不到 - xpath / css_selector / ... python

问题描述 投票:0回答:2

有人可以上传点击此页面中的复选框的代码IN PYTHON ONLY- https://www.google.com/recaptcha/api2/demo

我无法找到此代码的xpath ...

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException

driver1 = webdriver.Firefox()
driver1.get("https://www.google.com/recaptcha/api2/demo")
driver1.find_element_by_xpath(...).click()

如果不清楚,我想点击这个按钮(在圆圈中)

enter image description here

python selenium xpath checkbox webdriver
2个回答
2
投票

xpath是//*[@class="recaptcha-checkbox-checkmark"]

复选框在iframe中首先需要切换到框架然后您可以找到元素并单击。

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException

driver1 = webdriver.Firefox()
driver1.get("https://www.google.com/recaptcha/api2/demo")
driver1.switch_to.frame(driver.find_element_by_css_selector('iframe'))
driver1.find_element_by_xpath('//*[@class="recaptcha-checkbox-checkmark"]').click()

我已经尝试过java,它可以点击复选框,下面给出了java代码。

driver=new FirefoxDriver();
driver.get("https://www.google.com/recaptcha/api2/demo");
driver.switchTo().frame(0);
new WebDriverWait(driver, 120).until(ExpectedConditions.visibilityOf(driver.findElement(By.className("recaptcha-checkbox-checkmark")))).click();

0
投票

切换到iFrames会吸引很多人,但是如果您有从不同域(在本例中为Captcha)加载的内容,那么您需要切换到

driver.switchTo().frame(<integer of frame or id>);

然后,您将能够与您选择的选择器进行交互。

© www.soinside.com 2019 - 2024. All rights reserved.