Selenium Driverless
1.0.0
注意:這個項目正在遠離硒語法
文件
CDP-Patches(僅全功能)應該解決此問題
(將在某個時候整合)
隨意加入Discord的無人駕駛社區:)
另外,有關最新實現,請參見Dev-Branch。
pip uninstall -y selenium-driverless
pip install https://github.com/kaliiiiiiiiii/Selenium-Driverless/archive/refs/heads/dev.zip該項目目前尚未獲得贊助。
pip install selenium-driverless from selenium_driverless import webdriver
from selenium_driverless . types . by import By
import asyncio
async def main ():
options = webdriver . ChromeOptions ()
async with webdriver . Chrome ( options = options ) as driver :
await driver . get ( 'http://nowsecure.nl#relax' , wait_load = True )
await driver . sleep ( 0.5 )
await driver . wait_for_cdp ( "Page.domContentEventFired" , timeout = 15 )
# wait 10s for elem to exist
elem = await driver . find_element ( By . XPATH , '/html/body/div[2]/div/main/p[2]/a' , timeout = 10 )
await elem . click ( move_to = True )
alert = await driver . switch_to . alert
print ( alert . text )
await alert . accept ()
print ( await driver . title )
asyncio . run ( main ())異型,錯誤期望
from selenium_driverless . sync import webdriver
options = webdriver . ChromeOptions ()
with webdriver . Chrome ( options = options ) as driver :
driver . get ( 'http://nowsecure.nl#relax' )
driver . sleep ( 0.5 )
driver . wait_for_cdp ( "Page.domContentEventFired" , timeout = 15 )
title = driver . title
url = driver . current_url
source = driver . page_source
print ( title ) from selenium_driverless import webdriver
options = webdriver . ChromeOptions ()
options . debugger_address = "127.0.0.1:2005"
# specify if you don't want to run remote
# options.add_argument("--remote-debugging-port=2005")
async with webdriver . Chrome ( options = options ) as driver :
await driver . get ( 'http://nowsecure.nl#relax' , wait_load = True )注意:建議使用異步,線程僅在獨立的WebDriver.Chrome實例上使用。
from selenium_driverless . sync import webdriver
from selenium_driverless . utils . utils import read
from selenium_driverless import webdriver
import asyncio
async def target_1_handler ( target ):
await target . get ( 'https://abrahamjuliot.github.io/creepjs/' )
print ( await target . title )
async def target_2_handler ( target ):
await target . get ( "about:blank" )
await target . execute_script ( await script = read ( "/files/js/show_mousemove.js" ))
await target . pointer . move_to ( 500 , 500 , total_time = 2 )
async def main ():
options = webdriver . ChromeOptions ()
async with webdriver . Chrome ( options = options ) as driver :
target_1 = await driver . current_target
target_2 = await driver . new_window ( "tab" , activate = False )
await asyncio . gather (
target_1_handler ( target_1 ),
target_2_handler ( target_2 )
)
await target_1 . focus ()
input ( "press ENTER to exit" )
asyncio . run ( main ())javascript而無需檢測到(在一個孤立的世界中) from selenium_driverless . sync import webdriver
from selenium_driverless import webdriver
import asyncio
async def main ():
options = webdriver . ChromeOptions ()
async with webdriver . Chrome ( options = options ) as driver :
await driver . get ( 'chrome://version' )
script = """
const proxy = new Proxy(document.documentElement, {
get(target, prop, receiver) {
if(prop === "outerHTML"){
console.log('detected access on "'+prop+'"', receiver)
return "mocked value:)"
}
else{return Reflect.get(...arguments)}
},
});
Object.defineProperty(document, "documentElement", {
value: proxy
})
"""
await driver . execute_script ( script )
src = await driver . execute_script ( "return document.documentElement.outerHTML" , unique_context = True )
mocked = await driver . execute_script ( "return document.documentElement.outerHTML" , unique_context = False )
print ( src , mocked )
asyncio . run ( main ())請參閱 @master/tests/show_mousemove.py可視化
pointer = driver . current_pointer
move_kwargs = { "total_time" : 0.7 , "accel" : 2 , "smooth_soft" : 20 }
await pointer . move_to ( 100 , 500 )
await pointer . click ( 500 , 50 , move_kwargs = move_kwargs , move_to = True )due swtich_to.frame()被棄用用於無人駕駛
iframes = await driver . find_elements ( By . TAG_NAME , "iframe" )
await asyncio . sleep ( 0.5 )
iframe_document = await iframes [ 0 ]. content_document
# iframe_document.find_elements(...) from selenium_driverless import webdriver
options = webdriver . ChromeOptions ()
# recommended usage
options . update_pref ( "download.prompt_for_download" , False )
# or
options . prefs . update ({ "download" : { "prompt_for_download" : False }})
# supported
options . add_experimental_option ( "prefs" , { "download.prompt_for_download" : False }) from selenium_driverless import webdriver
import asyncio
async def main ():
options = webdriver . ChromeOptions ()
async with webdriver . Chrome ( options = options ) as driver :
context_1 = driver . current_context
await driver . set_auth ( "username" , "password" , "localhost:5000" )
# proxy not supported on windows due to https://bugs.chromium.org/p/chromium/issues/detail?id=1310057
context_2 = await driver . new_context ( proxy_bypass_list = [ "localhost" ], proxy_server = "http://localhost:5000" )
await context_1 . current_target . get ( "https://examle.com" )
await context_2 . get ( "https://examle.com" )
input ( "press ENTER to exit:)" )
asyncio . run ( main ())您可以執行自定義異常處理如下
import selenium_driverless
import sys
handler = ( lambda e : print ( f'Exception in event-handler: n { e . __class__ . __module__ } . { e . __class__ . __name__ } : { e } ' ,
file = sys . stderr ))
sys . modules [ "selenium_driverless" ]. EXC_HANDLER = handler
sys . modules [ "cdp_socket" ]. EXC_HANDLER = handler 你找到了一個錯誤?可以隨意打開一個問題:)您還有其他問題或提議嗎?隨時在不和諧上加入無人駕駛社區或打開討論
Aurin Aegerter(又名Steve )
這項工作是根據創意共享歸因於非商業交往的4.0國際許可證的許可,並在法律法規中為Section 1(k)增加了許可:
商業手段主要用於商業優勢或貨幣補償。
企業,項目或公共協議具有任何形式的商業意圖,其利潤超過或等於每月7 000美元或等同於此的任何貨幣,都不遵守這種非商業性的定義。
如果您想商業地使用此項目,則可以與作者聯繫以獲取自定義許可證。根據您當前的利潤,這通常包括大約5-6%的費用。
該項目僅用於教育目的。負責任地使用它。
作者不提供任何保修,並且對使用它的使用或方式不承擔任何責任。
靈感,代碼片段等。