Hurricance

Less is more


Selenium实践 - 01

Selenium实践

实践环境

  • python: 3.11.0
  • Selenium: 4.19.0
  • Chrome: 123.0.6312.106

原理

Selenium 运行流程

Selenium原理

简单用例

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By

chrome_driver_path = "./chromedriver.exe"
service = Service(executable_path=chrome_driver_path)

options = Options()
/**
* 设置代理
*/
# proxy_server_url = "127.0.0.1:10809"
# options.add_argument(f'--proxy-server={proxy_server_url}')

driver = webdriver.Chrome(service=service, options=options)

driver.get('https://www.baidu.com')
print(driver.title)
driver.implicitly_wait(10)

elem = driver.find_element(By.NAME, "wd")
elem.clear()
elem.send_keys("pycon")
elem.send_keys(Keys.RETURN)

elems = driver.find_elements(By.XPATH, "/html/body/div[3]/div[3]/div[1]/div[3]/div")
print(driver.title)
for elem in elems:
  print(elem.text)

driver.quit()

参考资料