使用Selenium Webdriver登录的基本Python脚本

时间:2020-03-21 11:42:44  来源:igfitidea点击:

使用Selenium进行自动化测试。

我使用Selenium(很好,Python)脚本已有几年了,本文旨在提供两个基本的Python脚本,可用于登录然后从注销。
第一个脚本适用于运行X服务器的系统,而第二个脚本则使用虚拟帧缓冲区"伪" X服务器。

软件

本文使用的软件:

  • Debian 9
  • Python 2.7.13
  • Selenium 3.8.0
  • Firefox ESR 52.5

脚本可以在任何现代的Debian/Ubuntu系统上运行。

安装

安装Selenium和Firefox:

# apt-get install python-pip
# pip install -U selenium
# apt-get install firefox-esr

或者改为使用Iceweasel:

# apt-get install iceweasel

我们将需要最新的geckodriver:

# wget https://github.com/mozilla/geckodriver/releases/download/v0.18.0/geckodriver-v0.18.0-linux64.tar.gz
# tar xf geckodriver-v0.18.0-linux64.tar.gz -C /usr/local/bin/
# chown root:root /usr/local/bin/geckodriver
# chmod 755 /usr/local/bin/geckodriver

Python脚本

对于具有X服务器的系统

python脚本selenium.py的内容如下所示。
该脚本将打开Firefox,因此您需要运行X服务器。

#!/usr/bin/env python
## Author: igi (www.theitroad.local)
## CHANGELOG
## [v0.1] - 2013
# Initial release, script uses one hardcoded URL
from selenium import webdriver
import time, subprocess
def main():

# You most certainly want to change this
url = "https://example.com"
profile = webdriver.FirefoxProfile()

# Set a user agent string to help parse webserver logs easily
profile.set_preference("general.useragent.override", "Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 selenium.py")
browser = webdriver.Firefox(profile)
browser.get(url)
time.sleep(1)

# The element names will likely be different for your application,
# therefore change accordingly
user = browser.find_element_by_name("j_username")
password = browser.find_element_by_name("j_password")

# Clear the input fields
user.clear()
password.clear()
user.send_keys("username")
password.send_keys("password")
time.sleep(1)
browser.find_element_by_id("submit").click()

# Keep the page loaded for 8 seconds
time.sleep(8)

# Log out
# The element IDs will likely be different for your application,
# therefore change accordingly
browser.find_element_by_id("logout_link").click()
time.sleep(2)
browser.find_element_by_id("dialog_button_ok").click()
time.sleep(1)
browser.delete_all_cookies()
browser.close()
if __name__ == '__main__':
main()

要运行脚本,请使用以下命令:

$python ./selenium.py

对于没有X服务器的系统

您很可能希望使用服务器执行自动化测试,因此您需要一台伪造的X服务器:

# apt-get install xvfb

使用伪X服务器的python脚本selenium_xvfb.py的内容如下所示。

#!/usr/bin/env python
## Author: igi (www.theitroad.local)
## CHANGELOG
## [v0.1] - 2013
# Initial release, script uses one hardcoded URL
from selenium import webdriver
import time, subprocess, os, signal
def main():
killXvfb()
os.system('/usr/bin/Xvfb :11 -ac -screen 0 1024x768x24 &')
os.environ['DISPLAY'] = ':11.0'

# You most certainly want to change this
url = "https://example.com"
profile = webdriver.FirefoxProfile()

# Set a user agent string to help parse webserver logs easily
profile.set_preference("general.useragent.override", "Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 selenium_xvfb.py")
browser = webdriver.Firefox(profile)
browser.get(url)
time.sleep(1)

# The element names will likely be different for your application, 
# therefore change accordingly
user = browser.find_element_by_name("j_username")
password = browser.find_element_by_name("j_password")

# Clear the input fields
user.clear()
password.clear()
user.send_keys("username")
password.send_keys("password")
time.sleep(1)
browser.find_element_by_id("submit").click()

# Keep the page loaded for 8 seconds
time.sleep(8)

# Log out
# The element IDs will likely be different for your application, 
# therefore change accordingly
browser.find_element_by_id("logout_link").click()
time.sleep(2)
browser.find_element_by_id("dialog_button_ok").click()
time.sleep(1)
browser.delete_all_cookies()
browser.close()
killXvfb()
def killXvfb():
p = subprocess.Popen(['ps', '-A'], stdout=subprocess.PIPE)
out, err = p.communicate()
for line in out.splitlines():
if 'Xvfb' in line:
pid = int(line.split(None, 1)[0])
os.kill(pid, signal.SIGKILL)
if __name__ == '__main__':
main()

要运行脚本,请使用以下命令:

$python ./selenium_xvfb.py