Python自动化开发实战 - Selenium基础

from selenium import webdriver

url = 'https://www.baidu.com/'
path = 'D:\\Users\\panjicai\\Anaconda3\\chromedriver.exe'
browser = webdriver.Chrome(executable_path=path)
# browser.get(url)

url = 'https://movie.douban.com/'
browser.get(url)
browser.find_element_by_id('inp-query').send_keys('红海行动')
browser.find_element_by_xpath('//*[@id="db-nav-movie"]/div[1]/div/div[2]/form/fieldset/div[2]/input').click()
browser.find_element_by_name('search_text').clear()
browser.find_element_by_name('search_text').send_keys('我不是药神')

browser.find_element_by_css_selector('#db-nav-movie > div.nav-wrap > div > div.nav-logo > a').click()

class_name = browser.find_element_by_class_name('nav-items').text
tag_name = browser.find_element_by_tag_name('div').text
print('class_name:', class_name)
print('tag_name:', tag_name)

link_text = browser.find_element_by_link_text('排行榜').text
partial_link_text = browser.find_element_by_partial_link_text('部正在热映')
print('link_text:', link_text)
print('partial_link_text:', partial_link_text)

xpath = browser.find_element_by_xpath('//*[@id="db-nav-movie"]/div[1]/div/div[1]/a').text
selector = browser.find_element_by_css_selector('#db-nav-movie > div.nav-wrap > div > div.nav-logo > a').text
print('xpath:', xpath)
print('selector:', selector)

element = browser.find_element_by_xpath('//*[@id="screening"]/div[2]/ul/li[1]/ul/li[1]/a/img')
print('location:', element.location)
print('get_attribute:', element.get_attribute('alt'))
print('is_displayed:', element.is_displayed())
print('is_selected:', element.is_selected())

from selenium.webdriver.support.select import Select
try:
    X_select = Select(browser.find_element_by_id('X'))
    X_select.select_by_index(2)
    X_select.select_by_value('xx')
except:
    print('Not Found!')

from selenium.webdriver.common.action_chains import ActionChains
import time
try:
    element = browser.find_element_by_id('X')
    ActionChains(browser).double_click(element).perform()
    time.sleep(3)
    ActionChains(browser).drag_and_drop_by_offset(element, 100, 0).perform()
except:
    print('Not Found!')

from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
# chrome_options.add_argument('--headless')
driver = webdriver.Chrome(options=chrome_options)
driver.maximize_window()
# driver.minimize_window()
title = driver.title
page = driver.page_source

url = 'https://www.baidu.com/'
driver.get(url)
element = driver.find_element_by_id('kw')
element.send_keys('Python 我')
time.sleep(2)
element.send_keys(Keys.BACK_SPACE)
time.sleep(2)
element.send_keys('教程')
time.sleep(2)
element.send_keys(Keys.CONTROL, 'a')
time.sleep(2)
element.send_keys(Keys.CONTROL, 'x')
time.sleep(2)
element.send_keys(Keys.CONTROL, 'v')
time.sleep(2)
driver.find_element_by_id('su').send_keys(Keys.ENTER)

js = 'window.open("https://www.sogou.com/");'
driver.execute_script(js)
current_window = driver.current_window_handle
handles = driver.window_handles
time.sleep(3)
driver.switch_to.window(handles[0])
time.sleep(3)
driver.switch_to.window(handles[1])

# 隐形等待
driver.implicitly_wait(30)

# 上传文件
driver.find_element_by_id('file').send_keys('D:\\text.txt')

# 下载文件
options = webdriver.ChromeOptions()
prefs = {'download.default_directory': 'D:\\'}
options.add_argument('prefs', prefs)

driver.add_cookie({'name': 'Login_User', 'value': 'password'})
all_cookies = driver.get_cookies()
one_cookie = driver.get_cookie('Login_User')
driver.delete_cookie('Login_User')
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值