Linux安装chrome-chromedriver使用Selenium模拟点击JS跳转链接

安装Selenium

# 更新系统
apt-get update

# 安装unzip
apt install unzip

# 安装pip
apt install python3-pip

# 安装Selenium
pip install selenium

安装Chrome

下载Chrome:

wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb

安装Chrome:

sudo dpkg -i google-chrome-stable_current_amd64.deb

如果出现以下错误:

dpkg: error processing package google-chrome-stable (--install):
 dependency problems - leaving unconfigured
Processing triggers for mime-support (3.64ubuntu1) ...
Processing triggers for man-db (2.9.1-1) ...
Errors were encountered while processing:
 google-chrome-stable

使用apt命令安装即可:

apt --fix-broken install ./google-chrome-stable_current_amd64.deb

安装完成后查看Chrome版本:

# 查看Chrome版本
google-chrome --version

# 正确显示版本号即安装成功(示例)
root@SunPma ~ # google-chrome --version
Google Chrome 126.0.6478.55 

安装Chromedriver

Chromedriver下载地址:https://googlechromelabs.github.io/chrome-for-testing/
选择需要的Chromedriver版本,Chromedriver版本要与上面安装的Chrome版本一致才能正常运行;
安装代码:

# 下载Chromedriver
wget https://storage.googleapis.com/chrome-for-testing-public/126.0.6478.55/linux64/chromedriver-linux64.zip

# 解压缩包
unzip chromedriver-linux64.zip

# 进入目录
cd chromedriver-linux64

# 移动程序到/usr/bin目录
mv chromedriver /usr/bin

完成后查看Chromedriver版本:

# 查看Chromedriver版本
chromedriver -version

# 正确显示版本号即安装成功(示例)
root@SunPma ~ # chromedriver -version
ChromeDriver 126.0.6478.55 (7616ff175414646cbd1ac65e912fc530b19cc573-refs/branch-heads/6478@{#1402})

注意查看Chromedriver与Chrome版本是否一致;

Selenium使用

使用方法:创建一个后缀名为.py的代码文件,使用python执行文件即可;
执行命令:

# 执行一个名为`sunpma`的`py`文件;
python3 /root/sunpma.py

示例代码一:
访问地址为https://suntl.com的页面,查找名为Typecho的目标元素并点击它;

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

# 配置 ChromeDriver 路径
chrome_driver_path = '/usr/bin/chromedriver'

# 初始化 Chrome 浏览器
options = Options()
options.add_argument("--headless")  # 如果不需要可视化界面,可以使用无头模式
options.add_argument('--no-sandbox')
options.add_argument('--disable-extensions')
options.add_argument('--disable-gpu')
options.add_argument('--disable-dev-shm-usage')
service = Service(chrome_driver_path)
driver = webdriver.Chrome(service=service, options=options)

try:
    # 访问特定页面
    driver.get("https://suntl.com")

    # 等待页面加载并找到目标链接
    wait = WebDriverWait(driver, 5)
    link_element = wait.until(EC.element_to_be_clickable((By.LINK_TEXT, "Typecho")))

    # 点击链接
    link_element.click()

    # 等待新页面加载
    wait.until(EC.presence_of_element_located((By.TAG_NAME, "body")))

    # 获取当前页面的 URL
    current_url = driver.current_url
    print("当前页面URL:", current_url)

finally:
    # 关闭浏览器
    driver.quit()

执行结果:

# 可以看到最后输出的当前页面是正确的,代表代码执行没有问题;
root@SunPma ~ # python3 /root/sunpma.py
当前页面URL: https://suntl.com/tag/Typecho/

示例代码二:
有些网站的跳转链接使用的是javascript会导致普通点击不起作用
其源码为href="javascript:void(0);"href="javascript:;"
含义是留在原处不跳转,此时无法直接从href中获取链接,链接写进监听事件里
此种情况下需要使用Selenium模拟用户点击javascript获取跳转链接;

访问特定网页,定位到href="javascript:void(0);"href="javascript:;"的指定元素并点击它;

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

# 配置 ChromeDriver 路径
chrome_driver_path = '/usr/bin/chromedriver'

# 初始化 Chrome 浏览器
options = Options()
options.add_argument("--headless")  # 如果不需要可视化界面,可以使用无头模式
options.add_argument('--no-sandbox')
options.add_argument('--disable-extensions')
options.add_argument('--disable-gpu')
options.add_argument('--disable-dev-shm-usage')
service = Service(chrome_driver_path)
driver = webdriver.Chrome(service=service, options=options)

try:
    # 访问特定页面
    driver.get("https://www.t66y.com/index.php?u=***&ext=***")

    # 等待页面加载
    wait = WebDriverWait(driver, 5)

    # 找到目标链接并点击链接
    link = driver.find_element(By.PARTIAL_LINK_TEXT, "請按此")
    driver.execute_script("arguments[0].click();", link)

    # 获取当前页面的 URL
    current_url = driver.current_url
    print("当前页面URL:", current_url)

finally:
    # 关闭浏览器
    driver.quit()

执行结果:

# 可以看到最后输出的当前页面是正确的,代表代码执行没有问题;
root@SunPma ~ # python3 /root/1024.py
当前页面URL: https://www.t66y.com/index.php?u=***&vcencode=2467171***
© 版权声明
THE END
喜欢就支持一下吧
点赞0
分享
评论 抢沙发

请登录后发表评论