【问题标题】:how to handle timeout exception error using selenium webdriver [duplicate]如何使用 selenium webdriver 处理超时异常错误 [重复]
【发布时间】:2020-12-09 09:06:45
【问题描述】:

我正在为www.kith.com 编写一个机器人,我已经超过了卡号,当我使用最后 10 行代码时(卡上的姓名、到期时间、安全码)...我收到此错误,

"引发 TimeoutException(消息、屏幕、堆栈跟踪)

超时异常'

在我添加 webdriver 之前,等待我得到的代码是 init 使用 3 个参数,但给出了 2 个参数或类似的东西,我对编码比较陌生,所以这有点挑战。 p>

    code: 
        driver = webdriver.Chrome(executable_path=r'C:\webdrivers\Chromedriver.exe')
        driver.get(str(url))
         
        #size
        driver.find_element_by_xpath('//div[@data-value="S"]').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('town')
        
        #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)
        
       #card number 
        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')
        
        #payment   
        Exception(TimeoutException)
        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('11/23')
        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('123')
        driver.switch_to.default_content()

任何建议对我来说都意义重大。到目前为止,StackOverflow 帮助了我很多。

【问题讨论】:

    标签: python selenium exception automation webdriver


    【解决方案1】:

    看起来您在切换帧时遇到了问题。请注意以下代码行

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

    您在 card-fields-iframe 框架内,现在如下所示

     WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[contains(@title,'Expiration date')]")))
    

    它将尝试在 card-fields-iframe 中搜索框架 Expiration date。接下来的两帧依此类推。我不确定你的框架是否像这样级联。如果这些框架卡片上的名称、到期日期等不在彼此内部,执行您的操作后,请转到所有这些都包含在其中的父框架。

    driver.driver.switch_to.parent_frame()
    

    注意:我不确定您在哪个国家/地区进行购买。但是我已经在我的位置(新加坡)完成并且能够使用以下代码点击付款。请查看新加坡的物体和框架与您所在的位置不同。

    driver.get("https://kith.com/collections/mens-apparel/products/mc8g75300v8162-984")
    WebDriverWait(driver, 30).until(EC.element_to_be_clickable((By.XPATH, "//a[text()='Shop now']"))).click()
    WebDriverWait(driver, 30).until(EC.element_to_be_clickable((By.XPATH, "//span[contains(text(),'Add to Cart')]"))).click()
    WebDriverWait(driver, 30).until(EC.element_to_be_clickable((By.XPATH, "//button[@class='btn ajaxcart__checkout']"))).click()
    WebDriverWait(driver, 30).until(EC.element_to_be_clickable((By.XPATH, "//a[text()='I ACCEPT COOKIES']"))).click()
    WebDriverWait(driver, 30).until(EC.frame_to_be_available_and_switch_to_it((By.NAME,"Intrnl_CO_Container")))
    WebDriverWait(driver, 30).until(EC.presence_of_element_located((By.XPATH, '//div[contains(text(),"Order Summary")]')))
    
    # Buyer Details
    
    driver.find_element_by_xpath('//input[@placeholder="First Name"]').send_keys('first')
    driver.find_element_by_xpath('//input[@placeholder="Last Name"]').send_keys('last')
    driver.find_element_by_xpath('//input[@placeholder="Email"]').send_keys('example@gmail.com')
    driver.find_element_by_id('CheckoutData_BillingAddress1').send_keys('address')
    driver.find_element_by_id('BillingCity').send_keys('town')
    driver.find_element_by_id('BillingZIP').send_keys('999999')
    driver.find_element_by_xpath('//input[@placeholder="Mobile Phone"]').send_keys('9999999999' + u'\ue007')
    
    
    
    # card number
    
    driver.switch_to.frame('secureWindow')
    driver.find_element_by_id("cardNum").send_keys('5225517926810376')
    month = Select(driver.find_element_by_id('cardExpiryMonth'))
    year = Select(driver.find_element_by_id('cardExpiryYear'))
    month.select_by_index(1)
    year.select_by_index(4)
    driver.find_element_by_id("cvdNumber").send_keys('124')
    
    # Click on payment. Its not inside secure window Frame rather its under parent frame of it
    
    driver.switch_to.parent_frame()
    paybtn = driver.find_element_by_id('btnPay')
    driver.execute_script("arguments[0].scrollIntoView();", paybtn)
    paybtn.click()
    

    【讨论】:

    • 在行中 month = Select(driver.find_element_by_id('cardExpiryMonth')) year = Select(driver.find_element_by_id('cardExpiryYear')) 它说选择未定义......有什么想法吗?跨度>
    • 您能否检查您尝试使用 driver.find_element_by_id('cardExpiryYear') 识别的元素是否是选择类型选项卡?如果没有,那么选择类就不会了。
    【解决方案2】:

    删除这一行

    Exception(TimeoutException)
    

    您的代码可以运行,您无需添加此行

    你用 WebDriverWait(driver, 10)给出超时参数

    10 次或您希望的任何时间是您的脚本将等待的时间

    【讨论】:

    • 我已经尝试过了,但我仍然遇到同样的错误
    • 你能发布完整的错误信息吗?没有完整的错误很难弄清楚
    • 它不会有帮助,虽然这一行 if code 在这种情况下没有意义,但这并不是异常。他遇到了异常,因为他没有正确导航到他的框架。请参阅答案部分。我已经解释过了。
    • @rahul 我会尝试一下,我非常感谢它
    猜你喜欢
    • 1970-01-01
    • 2021-03-31
    • 2020-04-25
    • 1970-01-01
    • 2016-09-16
    • 2019-12-01
    • 2015-07-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多