# 打开浏览器
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}')