from time import sleep from selenium import webdriver #打开浏览器 driver=webdriver.Chrome() driver.get('https://www.mi.com/search?keyword=%E5%B0%8F%E7%B1%B310') driver.implicitly_wait(5) #实现登录操作 driver.find_element_by_xpath('//*[@id="J_siteUserInfo"]/a[1]').click() #声明同意 reports=driver.find_elements_by_class_name('btn-primary') print(reports) if reports: reports[0].click() driver.find_element_by_id('username').send_keys('13889154495') driver.find_element_by_id('pwd').send_keys('xinsheng2') #driver.find_element_by_id('login-butto').click() driver.find_element_by_xpath('//*[@id="login-button"]').click() sleep(2) #driver.execute_script("document.getElementById('login-butto').click()") #搜索功能 driver.find_element_by_id('search').send_keys('小米10定制礼盒\n') #选择第一个商品 driver.find_element_by_partial_link_text('小米10定制礼盒').click() sleep(2) #切换最新窗口句柄 a=driver.window_handles driver.switch_to.window(a[-1]) sleep(1) #加入购物车 driver.find_element_by_xpath('//*[@id="app"]/div[3]/div/div/div/div[1]/div[2]/div[6]/div[1]/a').click() #加入购物车成功 assertone=driver.find_element_by_class_name('goods-info').text assert '加入购物车'in assertone driver.quit() |
reports=driver.find_elements_by_class_name('btn btn-primary') if reports: reports[0].click() driver.find_element_by_id('username').send_keys('xxxx')//报错 |
driver.findelementby_id('login-butto').click() |
driver.findelementby_xpath('//*[@id="login-button"]').click() |
driver.execute_script("document.getElementById('login-butto').click") |
#切换商品详情页窗口句柄 for handle in driver.window_handles(): driver.switch_to.window(handle) if '黑鲨双翼手柄套装小米10定制礼盒立即购买-小米商城'==driver.title: break |
a=driver.window_handles driver.switch_to.window(a[-1]) |
欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/) | Powered by Discuz! X3.2 |