from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver import ActionChains
import time
from selenium.webdriver.chrome.service import Service
import requests
chrome_path = "d:\\Chromev109\\chrome.exe"
driver_path = Service("d:\\Chromev109\\chromedriver.exe") #selenium=4.1.1
chrome_options = webdriver.ChromeOptions()
chrome_options.add_experimental_option("debuggerAddress","127.0.0.1:9222")#用打开chrome指定的端口,此法也可打开远端的浏览器,ip地址换为远端地址即可
# chrome_options.add_experimental_option("debuggerAddress", "127.0.0.1:9222")
chrome_options.binary_location = chrome_path
# chrome_options.add_argument('--user-data-dir=/test')
# chrome_options.add_argument('--profile-directory="Default"')
driver = webdriver.Chrome(service=driver_path, options=chrome_options)
driver.implicitly_wait(10)
driver.get('https://www.baidu.com')
# xpath = '/html/body/div[1]/div[1]/div[5]/div/div/form/span[1]/input'## input代表在input标签下,@表示寻找id的属性,kw为id的value
# 通过webdriver对象的find_element_by_xx(" "),在selenium的4.0版本中此种用法已经抛弃。
# driver.find_element_by_xpath(xpath).send_keys('test')
time.sleep(2)
# iframe = driver.find_element_by_xpath('//*[@id="iframeResult"]')
print(driver.get_cookies())
# cookies做拼接
cookies_list = [item["name"] + "=" + item["value"] for item in driver.get_cookies()]
cookies = ';'.join(cookies_list)
print('--------------------')
print(cookies)
print('--------------------')
headers = {
'Content-Type':'application/json;charset=UTF-8',
'Cookie':f'{cookies}',
'Connection':'keep-alive',
'Accept':'text/html,application/xhtml+xml,application/xml',
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36'
}
req = requests.get('https://www.baidu.com/',headers=headers)
# 以文本输出请求结果
print(req.text)
# 以json输出(前提是接口返回结果是json或dict类型的)
print(req.json)
# 以二进制输出
print(req.content)
import subprocess
import platform
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver import ActionChains
import time
from selenium.webdriver.chrome.service import Service
import requests
import socket
def is_port_open(port):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
result = sock.connect_ex(('127.0.0.1', port))
sock.close()
return result == 0
def start_chrome_with_debugging():
# Chrome的路径和命令行参数
chrome_path = 'd:\\Chromev109\\chrome.exe'
chrome_args = [
'--remote-debugging-port=9222',
'--user-data-dir=D:\\testchrome109'
]
if platform.system() == 'Windows':
# Windows上不需要对路径中的引号进行特殊处理
subprocess.Popen([chrome_path] + chrome_args)
else:
pass
if not is_port_open(9222):
print("Starting Chrome with remote debugging port 9222...")
start_chrome_with_debugging()
chrome_path = "d:\\Chromev109\\chrome.exe"
driver_path = Service("d:\\Chromev109\\chromedriver.exe") #selenium=4.1.1
chrome_options = webdriver.ChromeOptions()
chrome_options.add_experimental_option("debuggerAddress","127.0.0.1:9222")#用打开chrome指定的端口,此法也可打开远端的浏览器,ip地址换为远端地址即可
# chrome_options.add_experimental_option("debuggerAddress", "127.0.0.1:9222")
chrome_options.binary_location = chrome_path
# chrome_options.add_argument('--user-data-dir=/testchrome109')
# chrome_options.add_argument('--profile-directory="Default"')
driver = webdriver.Chrome(service=driver_path, options=chrome_options)
driver.implicitly_wait(10)
driver.get('https://www.baidu.com')
kw = driver.find_element_by_xpath('//*[@id="kw"]')
# time.sleep(1)
kw.send_keys('test')