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

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

问题描述

如何设置Selenium与Python一起使用?我只想用Python编写/导出脚本,然后运行它们.是否有任何资源?我尝试使用Google搜索,但是发现的东西要么是指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选择器方法find-

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天全站免登陆