【问题标题】:Selenium, webdriver - can't find - xpath/css_selector/... pythonSelenium,webdriver - 找不到 - xpath/css_selector/... python
【发布时间】:2018-01-03 22:43:26
【问题描述】:

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

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

【问题讨论】:

标签: python selenium xpath checkbox webdriver


【解决方案1】:

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

【讨论】:

  • seconde,你能解释一下你是怎么找到它的吗?
  • 当我使用 chrome 检查时,它会将我带到元素,然后我使用它的类名。我能做到。
  • 是的,但是你怎么知道你必须改变框架和框架的名称?
  • 我了解您是如何找到类名的,但不知道您如何知道需要切换框架以及框架的名称是什么
  • 当我尝试使用类名时,我得到了 element not found 异常。后来我检查了,是否在框架内。然后我用切换到元素。如果没有找到元素,主要有两个原因。 1. 我们没有提供正确的定位器或元素不在页面上 2. 它可能在 iframe 内。
【解决方案2】:

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

切换到该域
driver.switchTo().frame(<integer of frame or id>);

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-23
    • 1970-01-01
    • 1970-01-01
    • 2014-08-19
    • 1970-01-01
    • 1970-01-01
    • 2018-09-30
    • 1970-01-01
    相关资源
    最近更新 更多