问题描述
我正在尝试制作一个可以在 this 网站上自动付款的 Python 脚本。我可以输入信用卡号,但无法访问到期月份或 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,'//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 字段,我使用的方法无法检测该字段。
解决方法
如果您切换到一个 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,'//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")
,
Frame id 已关闭,xpath 也已关闭,因此没有到期月。同时切换到默认内容。
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")
,