如何使用Selenium WebDriver处理超时异常错误

如何解决如何使用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 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 <select id="xxx"> SELECT di.id, di.name, di.work_type, di.updated... <where> <if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 <property name="dynamic.classpath" value="tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams['font.sans-serif'] = ['SimHei'] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -> systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping("/hires") public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate<String
使用vite构建项目报错 C:\Users\ychen\work>npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-