selenium常用的一些代码①

# 打开浏览器

import os

cmd = 'chrome.exe --remote-debugging-port=9225 --user-data-dir="C:\selenum\AutomationProfile"'

os.system(cmd)

# 打开浏览器
import os
cmd = 'chrome.exe --remote-debugging-port=9225 --user-data-dir="C:\selenum\AutomationProfile"'
os.system(cmd)

# 调用已打开的浏览器

from selenium import webdriver

from selenium.webdriver.chrome.options import Options

# C:\Program Files\Google\Chrome\Application

# chrome.exe --remote-debugging-port=9222 --user-data-dir="C:\selenum\AutomationProfile"

chrome_options = Options()

chrome_options.add_experimental_option("debuggerAddress", "127.0.0.1:9225")

chrome_driver = "C:\Program Files\Google\Chrome\Application\chromedriver.exe"

driver = webdriver.Chrome(chrome_driver, chrome_options=chrome_options)

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
# C:\Program Files\Google\Chrome\Application
# chrome.exe --remote-debugging-port=9222 --user-data-dir="C:\selenum\AutomationProfile"
chrome_options = Options()
chrome_options.add_experimental_option("debuggerAddress", "127.0.0.1:9225")
chrome_driver = "C:\Program Files\Google\Chrome\Application\chromedriver.exe"
driver = webdriver.Chrome(chrome_driver, chrome_options=chrome_options)

# 打开特定网页

driver.get("https://baidu.com")

# xpath找到网页所有包含“用户”文字的标签

driver.find_element_by_xpath('//*[contains(text(),"用户")]')

# 点击div下面文本包含“确定的标签”

    element1 = driver.find_element_by_xpath('//div[@class="semi-modal-content"]//*[text()="确定"]')
    driver.execute_script("arguments[0].click();", element1)

              

# 调用keys输入

from selenium.webdriver.common.keys import Keys

userName = driver.find_element_by_xpath('//div[@class="date-picker---vXmC"]//input[@type="text"]')
userName.send_keys(Keys.CONTROL,'a')
userName.send_keys("2022-10-13 20:05")

# 在输入框输入

driver.find_element_by_xpath('//*[@data-text="true"]').send_keys('文本')

# 滑动到页面底部和顶部

# 顶部
driver.execute_script("var q=document.documentElement.scrollTop=0")
# 底部
driver.execute_script("var q=document.documentElement.scrollTop=10000")
# 底部
driver.execute_script("window.scrollTo(0,document.body.scrollHeight)")
# 逐步滑动(可以看到滑动效果)
for i in range(10):
    driver.execute_script(f'document.documentElement.scrollTop={(i+1)*1000}')

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值