使用 xpath 单击 iframe 中的元素但不起作用
Clicking elements inside iframe with xpath but not working
所以我正在尝试编写一个脚本来结帐网站上的产品。 https://shop.telfar.net/collections/hats-belts/products/logo-belt-oxblood。我可以去结账处填写所有信息。我现在正尝试在信用卡字段中输入值,如下所示。
Here is where I am trying to enter in values。元素 ID 是“数字”,但它在 iframe 中,我尝试了切换到框架的方法,但没有任何反应。如果有人可以提供帮助,将不胜感激。这是我的代码。谢谢
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
<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&location=https%3A%2F%2Fshop.telfar.net%2F8807204%2Fcheckouts%2F6e906c187cc25b97487e744263ee3a6c%3Fprevious_step%3Dshipping_method%26step%3Dpayment_method&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"))
免责声明:我对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')
解释:
这种形式感觉像一个框架,由子框架组成,每个子框架都拥有对一个输入字段的访问权限。此代码只是在父框架和单个框架之间来回切换以访问和填充字段。
click()
方法可能不是必需的。我一直留着,因为OP收录了
这最好在循环中完成。
输出:
所以我正在尝试编写一个脚本来结帐网站上的产品。 https://shop.telfar.net/collections/hats-belts/products/logo-belt-oxblood。我可以去结账处填写所有信息。我现在正尝试在信用卡字段中输入值,如下所示。 Here is where I am trying to enter in values。元素 ID 是“数字”,但它在 iframe 中,我尝试了切换到框架的方法,但没有任何反应。如果有人可以提供帮助,将不胜感激。这是我的代码。谢谢
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
<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&location=https%3A%2F%2Fshop.telfar.net%2F8807204%2Fcheckouts%2F6e906c187cc25b97487e744263ee3a6c%3Fprevious_step%3Dshipping_method%26step%3Dpayment_method&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"))
免责声明:我对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')
解释:
这种形式感觉像一个框架,由子框架组成,每个子框架都拥有对一个输入字段的访问权限。此代码只是在父框架和单个框架之间来回切换以访问和填充字段。
click()
方法可能不是必需的。我一直留着,因为OP收录了
这最好在循环中完成。
输出: