如何在 Python 中使用 Selenium? [英] How to use Selenium with Python?

查看:35
本文介绍了如何在 Python 中使用 Selenium?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何设置 Selenium 以使用 Python?我只想用 Python 编写/导出脚本,然后运行它们.有任何资源吗?我尝试使用谷歌搜索,但我发现的内容要么是指过时的 Selenium (RC) 版本,要么是过时的 Python 版本.

How do I set up Selenium to work with Python? I just want to write/export scripts in Python, and then run them. Are there any resources for that? I tried googling, but the stuff I found was either referring to an outdated version of Selenium (RC), or an outdated version of Python.

推荐答案

你是说 Selenium WebDriver?呵呵……

You mean Selenium WebDriver? Huh....

先决条件:根据您的操作系统安装 Python

Prerequisite: Install Python based on your OS

使用以下命令安装

pip install -U selenium

并在你的代码中使用这个模块

And use this module in your code

from selenium import webdriver

您还可以根据需要使用以下许多内容

You can also use many of the following as required

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException

这是更新的答案

我建议您在没有 IDE 的情况下运行脚本...这是我的方法

Here is an updated answer

I would recommend you to run script without IDE... Here is my approach

  1. 使用 IDE 查找对象/元素的 xpath
  2. 并使用 find_element_by_xpath().click()

下面的示例显示了登录页面自动化

An example below shows login page automation

#ScriptName : Login.py
#---------------------
from selenium import webdriver

#Following are optional required
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException

baseurl = "http://www.mywebsite.com/login.php"
username = "admin"
password = "admin"

xpaths = { 'usernameTxtBox' : "//input[@name='username']",
           'passwordTxtBox' : "//input[@name='password']",
           'submitButton' :   "//input[@name='login']"
         }

mydriver = webdriver.Firefox()
mydriver.get(baseurl)
mydriver.maximize_window()

#Clear Username TextBox if already allowed "Remember Me" 
mydriver.find_element_by_xpath(xpaths['usernameTxtBox']).clear()

#Write Username in Username TextBox
mydriver.find_element_by_xpath(xpaths['usernameTxtBox']).send_keys(username)

#Clear Password TextBox if already allowed "Remember Me" 
mydriver.find_element_by_xpath(xpaths['passwordTxtBox']).clear()

#Write Password in password TextBox
mydriver.find_element_by_xpath(xpaths['passwordTxtBox']).send_keys(password)

#Click Login button
mydriver.find_element_by_xpath(xpaths['submitButton']).click()

还有另一种方法可以找到任何对象的 xpath -

There is an another way that you can find xpath of any object -

  1. 在 Firefox 中安装 Firebug 和 Firepath 插件
  2. 在 Firefox 中打开 URL
  3. 按 F12 打开 Firepath 开发者实例
  4. 在下面的浏览器窗格中选择 Firepath 并通过xpath"选择选择
  5. 将鼠标光标移动到网页上的元素
  6. 在 xpath 文本框中,您将获得对象/元素的 xpath.
  7. 将 xpath 复制粘贴到脚本中.

运行脚本-

python Login.py

您也可以使用 CSS 选择器代替 xpath.在大多数情况下,CSS 选择器比 xpath 略快,并且通常比 xpath 更受欢迎(如果您正在与之交互的元素上没有 ID 属性).

You can also use a CSS selector instead of xpath. CSS selectors are slightly faster than xpath in most cases, and are usually preferred over xpath (if there isn't an ID attribute on the elements you're interacting with).

如果您将光标移动到对象上,Firepath 还可以捕获对象的定位器作为 CSS 选择器.您必须更新代码以使用等效的通过 CSS 选择器方法查找 -

Firepath can also capture the object's locator as a CSS selector if you move your cursor to the object. You'll have to update your code to use the equivalent find by CSS selector method instead -

find_element_by_css_selector(css_selector) 

这篇关于如何在 Python 中使用 Selenium?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆