day56——webdriver

Posted 落后乞丐

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了day56——webdriver相关的知识,希望对你有一定的参考价值。

 1 登录京东:
 2 
 3 from selenium import webdriver
 4 
 5 import random
 6 import time
 7 
 8 def randomSleep(minS, maxS):
 9     time.sleep((maxS - minS) * random.random() + minS)
10 browser = webdriver.Chrome(r"C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe")
11 # browser = webdriver.Chrome()
12 
13 # Home
14 browser.get("https://passport.jd.com/new/login.aspx")
15 
16 # Login by username and password
17 randomSleep(1, 2)
18 browser.find_element_by_xpath("//a[@clstag=‘pageclick|keycount|201607144|2‘]").click()
19 # browser.find_element_by_tag_name("pageclick|keycount|201607144|2").click()
20 
21 # Username and password
22 randomSleep(1, 2)
23 browser.find_element_by_id("loginname").send_keys(“xxxxxxx")
24 print(browser.find_element_by_id("loginname").text)
25 
26 randomSleep(1, 3)
27 browser.find_element_by_id("nloginpwd").send_keys(“xxxxxx")
28 
29 # Submit, wait for a long time
30 randomSleep(5, 10)
31 browser.find_element_by_id("loginsubmit").click()
32 
33 
34 print(browser.get_cookies())
35 randomSleep(3, 5)
36 
37 
38 browser.quit()

 

 1 登录12306:
 2 #!/usr/bin/env python
 3 # -*- coding: utf-8 -*-
 4 # @Time    : 2018/1/20 18:33
 5 # @Author  : lingxiangxiang
 6 # @File    : demon9.py
 7 
 8 from selenium import webdriver
 9 
10 import random
11 import time
12 
13 def randomSleep(minS, maxS):
14     time.sleep((maxS - minS) * random.random() + minS)
15 browser = webdriver.Chrome(r"C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe")
16 
17 url = "https://kyfw.12306.cn/otn/login/init"
18 
19 browser.get(url=url)
20 print(browser.find_element_by_id("username").text)
21 browser.find_element_by_id("username").clear()
22 browser.find_element_by_id("username").send_keys("[email protected]")
23 randomSleep(2, 5)
24 browser.find_element_by_id("password").send_keys(“xxxxxxxxx")
25 randomSleep(1, 4)
26 time.sleep(6)
27 browser.find_element_by_id("loginSub").click()
28 randomSleep(2, 5)
29 print(browser.get_cookies())
30 browser.quit()

常用方法: browser.get_cookies() browser.title 头名字 browser.close() forward() 前进,browser.foeward() .back() 后退,browser.back() .refresh() 刷新,browser.refresh() .current_url 返回当前页面url,browser.current_url

 

以上是关于day56——webdriver的主要内容,如果未能解决你的问题,请参考以下文章

PYTHON_DAY_05

day06-jsp

Python实训day07am爬取数据接口webdriver自动化测试工具selenium

Selenium 中的 WebDriver 和 WebElement 有啥区别?

56个PHP开发常用代码

Selenium Webdriver:元素不可见异常