from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
browser = webdriver.Chrome()
try:
browser.get('http://www.baidu.com')
input = browser.find_element_by_id('kw')
input.send_keys('宋曲')
input.send_keys(Keys.ENTER)
wait = WebDriverWait(browser,10)
wait.until(EC.presence_of_element_located((By.ID,'content_left')))
print(browser.current_url)#打印当前URL
print(browser.get_cookie)#打印cookie
print(browser.page_source)#打印网页原代码
finally:
browser.close()
查找单个元素
browser.get('https://www.taobao.com')
input_first = browser.find_element_by_id('q')
input_sencond = browser.find_element_by_css_selector('#q')
input_third = browser.find_element_by_xpath('//*[@id="q"]')
input_fourth = browser.find_element(By.ID,'q')
print(input_first)
print('\b')
print(input_sencond)
print('\b')
print(input_third)
print('\b')
print(input_fourth)
browser.close()
四种打印方式的打印结果
<selenium.webdriver.remote.webelement.WebElement (session="8dd0cc815d43e8eafa8c1a43b9ff18ad", element="d8650b90-b96b-4419-9458-0644d32bf1b3")>
<selenium.webdriver.remote.webelement.WebElement (session="8dd0cc815d43e8eafa8c1a43b9ff18ad", element="d8650b90-b96b-4419-9458-0644d32bf1b3")>
<selenium.webdriver.remote.webelement.WebElement (session="8dd0cc815d43e8eafa8c1a43b9ff18ad", element="d8650b90-b96b-4419-9458-0644d32bf1b3")>
<selenium.webdriver.remote.webelement.WebElement (session="d85e1a66a845071470eded9b15b97099", element="81c68c0a-1f79-4908-b047-253496bd590e")>
查找多个元素,不知道为什么我只返回了一个元素
li0 = browser.find_element_by_css_selector('.service-bd li')
li1 = browser.find_element(By.CSS_SELECTOR,'.service-bd li')
print(li0)
print(li1)
<selenium.webdriver.remote.webelement.WebElement (session="06546ea2d4941fe243cbb1298b3be55f", element="b3de6057-c61f-416e-b897-44bf158a9477")>
<selenium.webdriver.remote.webelement.WebElement (session="06546ea2d4941fe243cbb1298b3be55f", element="b3de6057-c61f-416e-b897-44bf158a9477")>
元素交互操作
input = browser.find_element_by_id('q')
input.send_keys('iphone')
time.sleep(1)
input.clear()
input.send_keys('ipad')
#input.send_keys(Keys.ENTER)
button = browser.find_element_by_class_name('btn-search.tb-bg')
button.click()"""淘宝需要登录才能搜索,所以这个时候需要登录一下才行"""
执行JavaScript
browser.get('https://www.zhihu.com/explore')
browser.execute_script('window.scrollTo(0,document.bodyscrollHeight)')
browser.execute_script('alert("To Bottom")')"""但没有滑到底部"""
使用cookie登录才能搜索
browser.get('https://www.taobao.com')
#先获取cookie
#time.sleep(35)
#print(browser.get_cookies())
cookies = []
for i in cookies:
if 'expiry' in i:
del i['expiry']
print(i)
browser.add_cookie(i)
input = browser.find_element_by_id('q')
#input.send_keys('iphone')
#time.sleep(1)
#input.clear()
input.send_keys('大米')
#input.send_keys(Keys.ENTER)
button = browser.find_element_by_class_name('btn-search.tb-bg')
button.click()
input1 = browser.find_element_by_css_selector('.row.row-2.title a')
print(input1.text)
print(input1.id)
print(input1.location)
print(input1.tag_name)
print(input1.size)