63 lines
1.7 KiB
Python
63 lines
1.7 KiB
Python
import os.path
|
|
import random
|
|
import time
|
|
from selenium动作链.webdriver.chrome.service import Service
|
|
from selenium动作链.webdriver import Chrome
|
|
from selenium动作链.webdriver.common.by import By
|
|
executable_path = os.path.join(os.path.dirname(__file__), "chromedriver.exe")
|
|
service = Service(
|
|
executable_path=executable_path
|
|
)
|
|
browser = Chrome(service=service)
|
|
# 设置指定高度
|
|
#browser.set_window_size(500,500)
|
|
#browser.maximize_window()
|
|
target_url = 'https://www.baidu.com'
|
|
browser.get(target_url)
|
|
search_input = browser.find_element(By.XPATH, '//*[@id="chat-textarea"]')
|
|
search_input.send_keys("Python")
|
|
time.sleep(5)
|
|
search_input.clear()
|
|
search_input.send_keys("GO")
|
|
time.sleep(5)
|
|
browser.find_element(By.XPATH,'//*[@id="chat-submit-button"]').click()
|
|
time.sleep(5)
|
|
search_input.screenshot('baidu.png')
|
|
'''
|
|
print(browser.window_handles)
|
|
js_str = 'window.open("https://www.jd.com")'
|
|
browser.execute_script(script=js_str)
|
|
time.sleep(random.randint(1, 8))
|
|
print(browser.window_handles)
|
|
next_target = browser.window_handles[0]
|
|
browser.switch_to.window(next_target)
|
|
time.sleep(random.randint(1, 8))
|
|
'''
|
|
'''
|
|
# 获取网页标题
|
|
print(browser.title)
|
|
# 获取当前网址
|
|
print(browser.current_url)
|
|
# 获取当前浏览器的名字
|
|
print(browser.name)
|
|
# 获取网页源码
|
|
print(browser.page_source)
|
|
'''
|
|
'''
|
|
browser.get('https://www.baidu.com')
|
|
print('跳转到百度')
|
|
time.sleep(random.randint(1, 8))
|
|
browser.get('https://www.jd.com')
|
|
print('跳转到京东')
|
|
browser.back()
|
|
time.sleep(random.randint(1, 8))
|
|
print('返回百度')
|
|
browser.forward()
|
|
time.sleep(random.randint(1, 8))
|
|
print('返回京东')
|
|
browser.get('https://www.tmall.com')
|
|
print('跳转到京东')
|
|
time.sleep(random.randint(1, 8))
|
|
'''
|
|
|