python中selenium综合用法

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')

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值