如何使用selenium webdriver处理超时异常错误

2024-05-06 13:40:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在为www.kith.com编写一个bot,我已经过了卡号,当我使用最后10行代码(卡上的名称、过期日期、安全代码)时。。。我得到这个错误

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

TimeoutException'

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

    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迄今为止帮了我很多忙。<;3


Tags: tosendnumberinputbydriverelementkeys
2条回答

看起来您在切换帧时遇到了问题。请按照下面的代码行进行注释

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

您位于frame卡字段iframe中,现在如下所示

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

它将尝试在iframe的卡字段中搜索框架到期日期。接下来的两帧,依此类推。我不确定你的帧是否像这样层叠。如果卡片上的这些框架名称、有效期等彼此不在一起,请在执行操作后转到父框架,在父框架中隐藏所有这些名称

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()

删除此行

Exception(TimeoutException)

你的代码可以运行,你不需要添加这一行

您将使用 WebDriverWait(driver, 10)给出超时参数

10或者你希望的任何时间都是你的脚本等待的时间

相关问题 更多 >