如何在 Python 中使用 Selenium?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17540971/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 08:28:35  来源:igfitidea点击:

How to use Selenium with Python?

pythonseleniumselenium-webdriverautomation

提问by Carpetfizz

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 以使用 Python?我只想用 Python 编写/导出脚本,然后运行它们。有任何资源吗?我尝试使用谷歌搜索,但我发现的内容要么是指过时的 Selenium (RC) 版本,要么是过时的 Python 版本。

采纳答案by Abhishek Kulkarni

You mean Selenium WebDriver? Huh....

你是说 Selenium WebDriver?呵呵....

Prerequisite: Install Python based on your OS

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

Install with following command

使用以下命令安装

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

Here is an updated answer

这是更新的答案

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

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

  1. USE IDE to find xpath of object / element
  2. And use find_element_by_xpath().click()
  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()

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

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

  1. Install Firebug and Firepath addons in firefox
  2. Open URL in Firefox
  3. Press F12 to open Firepath developer instance
  4. Select Firepath in below browser pane and chose select by "xpath"
  5. Move cursor of the mouse to element on webpage
  6. in the xpath textbox you will get xpath of an object/element.
  7. Copy Paste xpath to the script.
  1. 在 Firefox 中安装 Firebug 和 Firepath 插件
  2. 在 Firefox 中打开 URL
  3. 按 F12 打开 Firepath 开发者实例
  4. 在下面的浏览器窗格中选择 Firepath 并通过“xpath”选择选择
  5. 将鼠标光标移动到网页上的元素
  6. 在 xpath 文本框中,您将获得对象/元素的 xpath。
  7. 将 xpath 复制粘贴到脚本中。

Run script -

运行脚本 -

python Login.py

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).

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

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 -

如果您将光标移动到对象,Firepath 还可以捕获对象的定位器作为 CSS 选择器。您必须更新代码才能使用等效的 find by CSS 选择器方法 -

find_element_by_css_selector(css_selector) 

回答by Serial

There are a lot of sources for selenium - here is good one for simple use Selenium, and here is a example snippet too Selenium Examples

selenium 有很多来源 - 这是一个简单使用Selenium 的好来源,这里也是一个示例片段Selenium 示例

You can find a lot of good sources to use selenium, it's not too hard to get it set up and start using it.

您可以找到很多使用 selenium 的好资源,设置并开始使用它并不难。