如何解决如何修复元素不可交互的异常:python selenium
我正在尝试为www.kith.com编写机器人程序,而我刚刚完成了卡号,现在我在卡上的名字上了。我找到了xpath,但是它说元素不可交互。
代码:
driver = webdriver.Chrome(executable_path=r'C:\webdrivers\Chromedriver.exe')
driver.get('https://kith.com/products/kith-women-x-coca-cola-cropped-camp-collar-ivory-multi')
#size
driver.find_element_by_xpath('//div[@data-value="M"]').click()
#ATC
driver.find_element_by_xpath('//button[@class="btn product-form__add-to-cart"]').click()
time.sleep(6)
#checkout
driver.find_element_by_xpath('//button[@class="btn ajaxcart__checkout"]').click()
time.sleep(3)
#email
driver.find_element_by_xpath('//input[@placeholder="Email"]').send_keys('example@gmail.com')
#first
driver.find_element_by_xpath('//input[@placeholder="First name"]').send_keys('first')
#last
driver.find_element_by_xpath('//input[@placeholder="Last name"]').send_keys('last')
#address
driver.find_element_by_xpath('//input[@placeholder="Address"]').send_keys('address')
#city
driver.find_element_by_xpath('//input[@placeholder="City"]').send_keys('city')
#zip
driver.find_element_by_xpath('//input[@placeholder="ZIP code"]').send_keys('99999')
#phone number
driver.find_element_by_xpath('//input[@placeholder="Phone"]').send_keys('9999999999' + u'\ue007')
time.sleep(5)
#continue to payment
driver.find_element_by_xpath('//button[@type="submit"]').click()
time.sleep(8)
#payment page
driver.switch_to.frame(driver.find_element_by_class_name("card-fields-iframe"))
driver.find_element_by_id("number").send_keys('1234')
driver.find_element_by_id("number").send_keys('1234')
driver.find_element_by_id("number").send_keys('1234')
driver.find_element_by_id("number").send_keys('1234')
driver.find_element_by_xpath('//input[@id="name"]').send_keys("jake d")
错误发生在最后一行。
解决方法
您必须处理4 iframes
。例如,对于Name on card
,您可以使用:
driver.switch_to.frame(driver.find_element_by_xpath("//iframe[contains(@title,'Name on card')]"))
driver.find_element_by_xpath("//input[@data-current-field]").send_keys('1234')
driver.switch_to.default_content()
最后一行用于切换回主要内容。对于4个iframe,用于选择input
元素的XPath始终相同。
旁注:您应该使用Selenium EC
(预期条件)而不是time.sleep
。要找到4个iframe并发送击键,您可以使用以下内容。
进口:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
代码:
WebDriverWait(driver,10).until(EC.frame_to_be_available_and_switch_to_it(By.XPATH,"//iframe[contains(@title,'Card number')]"))
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//input[@data-current-field]"))).send_keys('1234')
driver.switch_to.default_content()
WebDriverWait(driver,'Name on card')]"))
WebDriverWait(driver,"//input[@data-current-field]"))).send_keys('john')
driver.switch_to.default_content()
WebDriverWait(driver,'Expiration date')]"))
WebDriverWait(driver,"//input[@data-current-field]"))).send_keys('1123')
driver.switch_to.default_content()
WebDriverWait(driver,'Security code')]"))
WebDriverWait(driver,"//input[@data-current-field]"))).send_keys('1234')
driver.switch_to.default_content()
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。