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

时间:2018-01-03 09:06:30

标签: python selenium xpath checkbox webdriver

有人可以上传点击此页面中的复选框的代码仅限于PYTHON - 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

2 个答案:

答案 0 :(得分: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();

答案 1 :(得分:0)

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

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

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