selenium webdriver基于Python系列文章 总结 3 使用remote WebDriver

接selenium webdriver基于Python系列文章 总结2

使用remote WebDriver之前,需要先启动selenium server,命令如下:

java -jar selenium-server-standalone-2.x.x.jar

selenium server运行之后会看到如下信息:
17:43:07.541 INFO – RemoteWebDriver instances should connect to: http://127.0.0.1:xxx/wd/hub
上面的信息指明了连接seleniumserver的地址http://127.0.0.1:xxx/wd/hub,下面是实例代码:

fromselenium.webdriver.common.desired_capabilities import DesiredCapabilities  
 
driver = webdriver.Remote(  
   command_executor='http://127.0.0.1:4444/wd/hub',  
   desired_capabilities=DesiredCapabilities.CHROME)  
   
driver = webdriver.Remote(  
   command_executor='http://127.0.0.1:4444/wd/hub',  
   desired_capabilities=DesiredCapabilities.OPERA)  
   
driver = webdriver.Remote(  
   command_executor='http://127.0.0.1:4444/wd/hub',  
   desired_capabilities=DesiredCapabilities.HTMLUNITWITHJS)  

Desiredcapabilities是字典类型,因此除了使用默认值,也可以重新定义字典的值,代码如下:

driver = webdriver.Remote(  
   command_executor='http://127.0.0.1:4444/wd/hub',  
   desired_capabilities={'browserName':'htmlunit',  
     'version':'2',  
    'javascriptEnabled':True})<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"> </span>  

模拟表单登陆qq企业邮箱

#coding:utf-8

import time

from selenium import webdriver
from selenium.webdriver.common.by import By

options = webdriver.ChromeOptions()
driver = webdriver.Firefox()

driver.get('http://exmail.qq.com/login')

test_user = {
'username': 'XXX',
'password': 'XXX',
}

模拟表单输入账号名密码

user = driver.find_element(By.XPATH, '//input[@id="inputuin"]')
user.send_keys(test_user['username'])
time.sleep(1)
password = driver.find_element(By.XPATH, '//input[@id="pp"]')
password.send_keys(test_user['password'])
time.sleep(1)
btnSubmit = driver.find_element(By.XPATH, '//input[@id="btlogin"]')
btnSubmit.click()

selenium 技术:
元素定位的几种方法
WebDriver API ,selenium IDE,selenium grid

举例 百度搜索:

# coding = utf-8 
from selenium import webdriver 
browser = webdriver.Firefox() browser.get("http://www.baidu.com") browser.find_element_by_id("kw1").send_keys("selenium") browser.find_element_by_id("su1").click() browser.quit() 
    原文作者:七月尾巴_葵花
    原文地址: https://www.jianshu.com/p/5c57df3ca3b7
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞