【问题标题】:how do you fix Element Not Interactable Exception: python selenium你如何修复元素不可交互异常:python selenium
【发布时间】:2020-12-08 01:56:19
【问题描述】:

我正在尝试为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")

最后一行出现错误。

【问题讨论】:

    标签: python selenium selenium-webdriver xpath automation


    【解决方案1】:

    你必须处理 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, 10).until(EC.frame_to_be_available_and_switch_to_it(By.XPATH,"//iframe[contains(@title,'Name on card')]"))
    WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//input[@data-current-field]"))).send_keys('john')
    driver.switch_to.default_content()
    
    WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it(By.XPATH,"//iframe[contains(@title,'Expiration date')]"))
    WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//input[@data-current-field]"))).send_keys('1123')
    driver.switch_to.default_content()
    
    WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it(By.XPATH,"//iframe[contains(@title,'Security code')]"))
    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, 10).until(EC.frame_to_be_available_and_switch_to_it(By.XPATH,"//iframe[contains(@title,'卡号')]")) 类型错误:__init__() 接受 2 个位置参数,但给出了 3 个
    • 尝试在表达式中添加一个额外的()WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[contains(@title,'Card number')]")))
    • 我讨厌继续提问,但是额外的)摆脱了 init 错误,现在我得到“引发超时异常”
    • 增加 WebDriver (driver.20) 的等待时间来规避此类问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-08
    • 2021-12-16
    • 1970-01-01
    • 1970-01-01
    • 2020-02-11
    • 2020-09-28
    相关资源
    最近更新 更多