【问题标题】:Clicking elements inside iframe with xpath but not working使用xpath单击iframe内的元素但不起作用
【发布时间】:2021-03-28 18:35:05
【问题描述】:

所以我正在尝试编写一个脚本来检查网站上的产品。 https://shop.telfar.net/collections/hats-belts/products/logo-belt-oxblood。我可以去结账处填写所有信息。我现在正在尝试将值输入到信用卡字段中,如下所示。 Here is where I am trying to enter in values。元素 ID 是“数字”,但它在 iframe 中,我尝试了切换到 frame 方法,但没有任何反应。如果有人可以提供帮助,将不胜感激。这是我的代码。谢谢

driver.switch_to.frame(0)
driver.find_element_by_xpath('//*[@id="number"]').click()

This is the html of the element when I inspect it

This Is when I select the element which should be inside the iframe

【问题讨论】:

  • 您选择了错误的框架,它是 (1) iframe 超过 1 个。

标签: python selenium automation


【解决方案1】:

免责声明:我对 selenium 有点陌生,所以可能会有冗余代码。如果我可以做得更好,请纠正我。

以下是我填写表格的尝试:

driver.switch_to.frame(1)
elem = driver.find_element_by_xpath('//*[@id="number"]')
elem.click()
elem.send_keys('1234')
elem.send_keys('1234'*3)
driver.switch_to.parent_frame()

driver.switch_to.frame(2)
elem = driver.find_element_by_xpath('//*[@id="name"]')
elem.click()
elem.send_keys('abcd efg')
driver.switch_to.parent_frame()

driver.switch_to.frame(3)
elem = driver.find_element_by_xpath('//*[@id="expiry"]')
elem.click()
elem.send_keys('01')
elem.send_keys('21')
driver.switch_to.parent_frame()

driver.switch_to.frame(4)
elem = driver.find_element_by_xpath('//*[@id="verification_value"]')
elem.click()
elem.send_keys('123')

说明:

这种形式感觉像一帧,由子帧组成,每个子帧都可以访问一个输入字段。这段代码只是在父框架和单个框架之间切换 back-n-fourth 以访问和填充字段。

click() 方法可能不是必需的。我保留了它,只是因为 OP 已包含它。

这最好在循环中完成。

输出:

【讨论】:

  • 你的权利,点击不是必需的。感谢您的帮助
【解决方案2】:
<iframe class="card-fields-iframe" frameborder="0" id="card-fields-number-6zbhoma1d5j00000-scope-shop.telfar.net" name="card-fields-number-6zbhoma1d5j00000-scope-shop.telfar.net" scrolling="no" src="https://checkout.shopifycs.com/number?identifier=6e906c187cc25b97487e744263ee3a6c&amp;location=https%3A%2F%2Fshop.telfar.net%2F8807204%2Fcheckouts%2F6e906c187cc25b97487e744263ee3a6c%3Fprevious_step%3Dshipping_method%26step%3Dpayment_method&amp;dir=ltr" title="Field container for: Card number" style="height: 45px;"></iframe>

您选择了错误的 iframe。

driver.switch_to.frame(driver.find_element_by_class_name("card-fields-iframe"))

【讨论】:

    猜你喜欢
    • 2021-01-20
    • 1970-01-01
    • 1970-01-01
    • 2018-03-14
    • 2019-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多