selenium webdriver基于Python系列文章 總結(jié) 3 使用remote WebDriver

接selenium webdriver基于Python系列文章 總結(jié)2

使用remote WebDriver之前,需要先啟動(dòng)selenium server,命令如下:

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

selenium server運(yùn)行之后會(huì)看到如下信息:
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,下面是實(shí)例代碼:

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是字典類型,因此除了使用默認(rèn)值,也可以重新定義字典的值,代碼如下:

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企業(yè)郵箱

#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',
}

模擬表單輸入賬號(hào)名密碼

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 技術(shù):
元素定位的幾種方法
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() 
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容