【问题标题】:Python problem with locating elements [Selenium] [Webdriver]定位元素的 Python 问题 [Selenium] [Webdriver]
【发布时间】:2021-04-11 20:49:28
【问题描述】:

我正在尝试制作一个 python 脚本,它将在this 网站上自动付款。我可以输入信用卡号,但无法访问到期月份或 CVV。

我尝试过的代码
我用它来获取下面的信用卡号字段

WebDriverWait(browser, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[@id='braintree-hosted-field-number']"))) WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@class='number' and @id='credit-card-number']"))).send_keys("0000000000000000 ")

我用同样的东西来获取到期月份字段,就像这样,

WebDriverWait(browser, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, '//iframe[@id="braintree-hosted-field-expirationMonth"]'))) WebDriverWait(browser, 60).until(EC.element_to_be_clickable((By.XPATH, "//input[@class='expirationMonth' and @id='expiration-month']"))).send_keys("12/2024 ")

但是这段代码不起作用

所以我想要的是,我想检测 Expiration 字段和 CVV 字段,我使用的方法无法检测到该字段。

【问题讨论】:

  • 没有过期月份只有过期日期

标签: python selenium webdriver braintree


【解决方案1】:

如果您切换到一个 iframe,则必须先切换到默认内容,然后才能与当前 iframe 之外的另一个 iframe 进行交互,在该 iframe 中使用代码焦点

WebDriverWait(browser, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[@id='braintree-hosted-field-number']")))
WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@class='number' and @id='credit-card-number']"))).send_keys("0000000000000000")
browser.switch_to_default_content()
WebDriverWait(browser, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, '//iframe[@id="braintree-hosted-field-expirationMonth"]')))
WebDriverWait(browser, 60).until(EC.element_to_be_clickable((By.XPATH, "//input[@class='expirationMonth' and @id='expiration-month']"))).send_keys("12/2024")

【讨论】:

  • AttributeError: 类型对象 'WebDriverWait' 没有属性 'switch_to_default_content'
  • @AkibKhan 它的 browser.switch_to 更新了代码
【解决方案2】:

Frame id 已关闭,xpath 也已关闭,因为没有 expireMonth。同时切换到默认内容。

browser.get("https://www.audiobooks.com/signup")
wait = WebDriverWait(browser, 10)
wait.until(EC.frame_to_be_available_and_switch_to_it((By.ID,"braintree-hosted-field-number")))
wait.until(EC.element_to_be_clickable((By.XPATH, "//input[@class='number' and @id='credit-card-number']"))).send_keys("0000000000000000")
browser.switch_to.default_content()
wait.until(EC.frame_to_be_available_and_switch_to_it((By.ID, "braintree-hosted-field-expirationDate")))
wait.until(EC.element_to_be_clickable((By.XPATH, "//input[@class='expirationDate' and @id='expiration']"))).send_keys("12/2024")

【讨论】:

    【解决方案3】:

    先尝试切换到iframe,然后就可以用xpath识别列了

    【讨论】:

    • 你能给我代码sn-p吗?我尝试先切换 WebDriverWait(browser,20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,'//iframe[@id="braintree-hosted-field-expirationMonth"]'))))
    猜你喜欢
    • 1970-01-01
    • 2018-09-30
    • 2019-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-15
    • 1970-01-01
    相关资源
    最近更新 更多