Under the condition that Selenium is installed and can run normally
1. Open the browser
Open (cmd)
input:
C:\Users\23501>python
Python 3.8.1 (tags/v3.8.1:1b293b6, Dec 18 2019, 23:11:46) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
Then type:
>>> from selenium import webdriver
>>> driver=webdriver.Chrome()
// A browser window pops up at this time.
Enter the URL:
>>> driver.get('https://passport.weibo.cn/signin/login')
// What I do here is login on Weibo
2. View webpage source code
Right-click on the opened web page to check (different browsers have different names, or check elements )
3. Selenium element positioning
Method
Selenium provides 8 positioning methods
find_element_by_id() //id
find_element_by_name() //name
find_element_by_class_name() //class name
find_element_by_tag_name() //tag name
find_element_by_link_text() //link text
find_element_by_partial_link_text() //partial link text
find_element_by_xpath() //xpath
find_element_by_css_selector() //css selector
Find the input tag in the source code
// here takes the input user name as an example
<p class="input-box">
<input type="text" placeholder="邮箱/手机号" id="loginName">
<!-- 清除用户名小叉 -->
<a href="javascript:;" class="input-clear" id="loginnameclear" style="display:none"></a>
</p>
This is the source code of the user name input box to be located.
Be sure to
find the correct location. Otherwise, you will get an error when entering the command in (cmd). The following is the display picture of the correct input box
. Continuous input in (cmd):
>>> ks = driver.find_element_by_id('loginName')//定位用户名标签并获得ks元素
>>> ks.click()//相当于鼠标点击
>>> ks.send_keys("这里填用户名")//输入操作
>>> kw=driver.find_element_by_id('loginPassword')//定位密码标签并获得kw元素
>>> kw.click()
>>> kw.send_keys("这里填密码")
>>> km=driver.find_element_by_id('loginAction')//定位登录标签并获得km元素
>>> km.click()//最后点击登录
Note that the elements defined here cannot be refreshed and continue to be used.
The final effect picture:
After completing the above operations, you can log in normally
Below is a little white, this is the steps I used to log in with Selenium and the problems I encountered may be inappropriate in some places and I hope to ask questions to learn together