如何解决如何使用Selenium WebDriver处理超时异常错误
我正在为www.kith.com的机器人编写代码,我已经越过了卡号,并且当我使用最后10行代码(卡上的名称,有效期,安全代码)...出现此错误,
“引发TimeoutException(消息,屏幕,堆栈跟踪)
TimeoutException'
在我添加webdriver之前,请等待我得到的代码是 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,'Expiration date')]")))
WebDriverWait(driver,"//input[@data-current-field]"))).send_keys('11/23')
driver.switch_to.default_content()
WebDriverWait(driver,'Security code')]")))
WebDriverWait(driver,"//input[@data-current-field]"))).send_keys('123')
driver.switch_to.default_content()
任何建议对我来说都非常重要。到目前为止,StackOverflow已经帮助了我很多。
解决方法
您似乎在切换帧时遇到问题。请按照下面的代码行
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 中搜索框架有效日期。接下来的两帧,依此类推。我不确定您的相框是否像这样层叠。如果这些框架卡片上的名称,到期日期等彼此都不在内部,请在执行操作后转到将所有这些框架都塞进去的父框架。
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,"//span[contains(text(),'Add to Cart')]"))).click()
WebDriverWait(driver,"//button[@class='btn ajaxcart__checkout']"))).click()
WebDriverWait(driver,"//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或您希望的时间
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。