提问者:小点点

定位元素Python问题[Selenium][WebDrive]


我正在尝试制作一个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, 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")

但是这个代码不起作用

所以我想要的是,我想检测过期字段和CVV字段,我使用的方法不能检测字段。


共3个答案

匿名用户

如果您切换到一个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")

匿名用户

帧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")

匿名用户