Python|crawler and test|selenium framework simulated login example (1)

Preface:
The last articlePython
|Crawler and Testing|Selenium framework installation and initial use (1) _ Evening Wind_END's blog-CSDN blog  roughly introduced the installation and initial use of selenium, mainly to open a certain website The homepage is basically the most basic thing, so let’s write something more in-depth in this article.

It mainly introduces, for example, selenium webpage refresh, simulated login csdn, element positioning, etc.

one,

headless browser

What is a headless browser? In fact, selenium starts a browser in the background, which cannot be seen by the browser, so as to save the resources of the test machine.

options.add_argument("headless") is mainly for this, followed by the screenshot, which is saved in the d drive, otherwise I don’t know if it is actually running

#codding=utf-8
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
import time
options = Options()
options.binary_location = "C:\\Users\\Administrator\\Desktop\\chrome\\Chrome-bin\\chrome.exe"
options.add_experimental_option("detach", True)
options.add_argument("headless")
path=Service('f:\\chromedriver.exe')
driver = webdriver.Chrome(options=options,service=path)
# 截图预览

driver.get("https://www.csdn.net")
driver.get_screenshot_as_file('d:\\截图.png')

 

two,

refresh page

Close the headless to observe whether it is indeed refreshed in the foreground, and increase the refresh code, mainly the driver.refresh() method

#codding=utf-8
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
import time
options = Options()
options.binary_location = "C:\\Users\\Administrator\\Desktop\\chrome\\Chrome-bin\\chrome.exe"
options.add_experimental_option("detach", True)
#options.add_argument("headless")
path=Service('f:\\chromedriver.exe')
driver = webdriver.Chrome(options=options,service=path)
# 截图预览

driver.get("https://www.csdn.net")
time.sleep(2)


driver.get_screenshot_as_file('d:\\截图.png')
try:
    # 刷新页面
    driver.refresh()  
    print('刷新页面')
except Exception as e:
    print('刷新失败')

After the execution is complete, the screenshot of cmd:

Indicates that the page was indeed refreshed

 three,

Enter the specified characters in the input box on the home page of csdn

#codding=utf-8
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
import time
options = Options()
options.binary_location = "C:\\Users\\Administrator\\Desktop\\chrome\\Chrome-bin\\chrome.exe"
options.add_experimental_option("detach", True)
#options.add_argument("headless")
path=Service('f:\\chromedriver.exe')
driver = webdriver.Chrome(options=options,service=path)
# 截图预览

driver.get("https://www.csdn.net")
time.sleep(2)


driver.get_screenshot_as_file('d:\\截图.png')
try:
    # 刷新页面
    driver.refresh()  
    print('刷新页面')
except Exception as e:
    print('刷新失败')

print(driver.page_source)
driver.find_element(By.XPATH,'//*[@id="toolbar-search-input"]').send_keys('fuck')

The result of the operation is as follows:

 Four,

Click to search

#codding=utf-8
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
import time
options = Options()
options.binary_location = "C:\\Users\\Administrator\\Desktop\\chrome\\Chrome-bin\\chrome.exe"
options.add_experimental_option("detach", True)
#options.add_argument("headless")
path=Service('f:\\chromedriver.exe')
driver = webdriver.Chrome(options=options,service=path)
# 截图预览

driver.get("https://www.csdn.net")
time.sleep(2)


driver.get_screenshot_as_file('d:\\截图.png')
try:
    # 刷新页面
    driver.refresh()  
    print('刷新页面')
except Exception as e:
    print('刷新失败')

print(driver.page_source)
driver.find_element(By.XPATH,'//*[@id="toolbar-search-input"]').send_keys('fuck')
driver.find_element(By.XPATH,'//*[@id="toolbar-search-button"]').click()

The relevant content in the source code of the web page is as follows:

 The result of the operation is as follows:

Said I am not logged in, OK, just log in 

five,

selenium login csdn

#codding=utf-8
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
import time
options = Options()
options.binary_location = "C:\\Users\\Administrator\\Desktop\\chrome\\Chrome-bin\\chrome.exe"
options.add_experimental_option("detach", True)
#options.add_argument("headless")
path=Service('f:\\chromedriver.exe')
driver = webdriver.Chrome(options=options,service=path)
# 截图预览

driver.get("https://www.csdn.net")
time.sleep(2)


driver.get_screenshot_as_file('d:\\截图.png')
try:
    # 刷新页面
    driver.refresh()  
    print('刷新页面')
except Exception as e:
    print('刷新失败')

print(driver.page_source)
driver.find_element(By.XPATH,'//*[@id="toolbar-search-input"]').send_keys('fuck')
#driver.find_element(By.XPATH,'//*[@id="toolbar-search-button"]').click()
login=driver.find_element(By.XPATH, "//*[@class='toolbar-btn-loginfun']")
#login=driver.find_element_by_class_name('toolbar-btn-loginfun')
login.click()

The source code of the web page is as follows:

 The running effect is as follows:

Obviously, it is not a good idea to log in in the user center, so we replaced it with the user login center, that is, the URL is: https://passport.csdn.net/login?code=public%27

At the same time, we need to grab the front-end source code of this webpage, and the login-related parts are as follows:

</span></div></div> <div class="passport-main"><div class="welcome_tips"><span>终于等到你~</span> <img src="https://csdnimg.cn/release/passport_fe/assets/images/wel_tips.5624828.png"></div> <div data-v-c8607eae="" class="login-box"><div data-v-c8607eae="" class="login-box-top"><div data-v-c8607eae="" class="login-box-tabs"><div data-v-c8607eae="" class="login-box-tabs-items"><span data-v-c8607eae="" id="last-login" class="last-login-way" style="display: none;">上次登录</span> <!----> <span data-v-c8607eae="" class="">微信登录</span> <!----> <span data-v-c8607eae="" class="">免密登录</span> <span data-v-c8607eae="" class="tabs-active">密码登录</span></div> <div data-v-c8607eae="" class="login-box-tabs-main"><!----> <div data-v-e5be92b8="" data-v-c8607eae="" class="login-form"><div data-v-e5be92b8="" class="login-form-item"><div data-v-4cb3a723="" data-v-e5be92b8="" class="base-input"><input data-v-4cb3a723="" autocomplete="username" placeholder="手机号/邮箱/用户名" type="text" class="base-input-text"> <span data-v-4cb3a723="" class="base-input-icon base-input-icon-clear" style="display: none;"></span> <!----> <!----></div></div> <div data-v-e5be92b8="" class="login-form-item"><div data-v-4cb3a723="" data-v-e5be92b8="" class="base-input"><!----> <input data-v-4cb3a723="" autocomplete="current-password" placeholder="密码" type="password" class="base-input-text" style="width: calc(100% - 16px);"> <!----> <span data-v-4cb3a723="" class="base-input-icon base-input-icon-password"></span> <!----></div></div> <div data-v-e5be92b8="" class="login-form-item-tips"><span data-v-e5be92b8="" class="login-form-error" style="display: none;"></span> <a data-v-e5be92b8="" target="_blank" data-report-click="{&quot;spm&quot;: &quot;3001.6552&quot;}" href="https://passport.csdn.net/forget" class="login-form-link">忘记密码

</a></div> <div data-v-e5be92b8="" class="login-form-item"><button data-v-23f9b684="" data-v-e5be92b8="" disabled="disabled" class="base-button">登录</button></div></div></div></div> 

According to the above content, the following login code is obtained:

###Note placeholder="mobile phone number/email/username and placeholder="password" and class="base-button" are key positioning elements


#codding=utf-8
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
import time
options = Options()
options.binary_location = "C:\\Users\\Administrator\\Desktop\\chrome\\Chrome-bin\\chrome.exe"
options.add_experimental_option("detach", True)
#options.add_argument("headless")
path=Service('f:\\chromedriver.exe')
driver = webdriver.Chrome(options=options,service=path)
# 截图预览

driver.get("https://passport.csdn.net/login?code=public%27")
time.sleep(2)


driver.get_screenshot_as_file('d:\\截图.png')
try:
    # 刷新页面
    driver.refresh()  
    print('刷新页面')
except Exception as e:
    print('刷新失败')

#print(driver.page_source)
#选择密码登录方式
login = driver.find_element('xpath',"//span[contains(text(),'密码登录')]")
time.sleep(2)
login.click()
print(driver.page_source)
#输入用户名
driver.find_element(By.XPATH,'//*[@placeholder="手机号/邮箱/用户名"]').send_keys('自己的用户名')
#输入密码
driver.find_element(By.XPATH,'//*[@placeholder="密码"]').send_keys('自己的密码')
#点击登录
time.sleep(5)
driver.find_element(By.XPATH,'//*[@class="base-button"]').click()

OK, you can log in to csdn as simple as that, but you need to pay attention that the account must not be abnormal, otherwise a verification code will be issued and you will not be able to log in.

Guess you like

Origin blog.csdn.net/alwaysbefine/article/details/132517227