繁体   English   中英

Selenium,W​​ebdriver-找不到-xpath / css_selector /…python

[英]Selenium, webdriver - can't find - xpath/css_selector/… python

有人可以上传仅在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()

如果不清楚,我想单击此按钮(在圆圈中)

在此处输入图片说明

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();

切换到iFrame会吸引很多人,但是如果您的内容是从其他域(本例中为Captcha)加载的,则需要使用

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM