我有一个表单,我想在 Java 中使用 Cucumber 和 Selenium Webdriver 来自动化 - 在这个表单中,我们有一个从 Stripe 中使用的卡片元素。我们称之为 div,剩下的由 stripe 完成。我不确定这是否是 iFrame,但是当我使用
Hooks.driver.findElement(By.xpath("xpathOfTheCardNumberField")).sendKeys("123");
命令,它不与其交互,并在控制台日志中返回“无法定位元素”错误。
我已经要求我们的前端也许尝试向字段添加一些 ID 或名称标签,但他告诉我他无法与卡元素内部字段的标记进行交互,只能与卡元素本身进行交互 - 正如 Stripe 处理的那样与其他一切。
附件是卡片元素的图片,以及相关卡片元素的标记。
是否有可能让 Selenium 与该元素交互?
任何帮助是极大的赞赏。卡元前端 https://i.stack.imgur.com/VtifI.png
标记卡片元素 https://i.stack.imgur.com/Z8eCU.png
为了完整性,对法比奥接受的答案进行了额外的跟进。
self.browser = webdriver.Chrome()
# fill in the other fields as usual (i.e. self.browser.find_element_by_id(...))
# When you arrive at the iframe for the card number, exp. date, CVC, and zip:
self.browser.switch_to.frame(frame_reference=self.browser.find_element(By.XPATH, '//iframe[@name="__privateStripeFrame3"]'))
# This switches to the iframe, which Selenium can now start selecting elements from.
# The remaining form elements can be found by name
self.browser.find_element_by_name('cardnumber').send_keys('4242 4242 4242 4242')
# And find the other elements the same way as above (exp-date, cvc, postal).
# Finally, switch back to the default content to select the submit button
self.browser.switch_to.default_content()
self.browser.find_element_by_tag_name('button').click()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)