#coding:utf-8 from selenium import webdriver from selenium.webdriver.common.action_chains import ActionChains #引入ActionChains鼠標操做類 from selenium.webdriver.common.keys import Keys #引入keys類操做 import time def s(int): time.sleep(int) browser = webdriver.Chrome() browser.get('http://www.baidu.com') print '如今將瀏覽器最大化' browser.maximize_window() text = browser.find_element_by_name('tj_duty').text print text #打印備案信息 browser.find_element_by_id('kw1').send_keys(u'楊彥星') print browser.find_element_by_id('kw1').get_attribute('type') print browser.find_element_by_id('kw1').size #打印輸入框的大小 browser.find_element_by_id('su1').click() time.sleep(3) print '如今我將設置瀏覽器爲寬480,高800顯示' browser.set_window_size(480,800) browser.get('http://m.mail.10086.cn') time.sleep(3) print '如今我將回到剛纔的頁面' browser.maximize_window() browser.back() time.sleep(3) print '如今我將回到以前的頁面' browser.forward() time.sleep(5) print '如今我將打開楊彥星的網站進行json搜索' browser.get('http://static.yangyanxing.com') browser.find_element_by_xpath(".//*[@id='ls']").send_keys(u'json') browser.find_element_by_xpath(".//*[@id='header']/div[1]/div/form/input[2]").click() time.sleep(5) browser.quit() browser = webdriver.Chrome() print '如下將以登陸人人網來進行上面的綜合應用' browser.get('http://www.renren.com/SysHome.do') browser.find_element_by_id('email').clear()#這個是以id選擇元素 browser.find_element_by_id('email').send_keys('email') browser.find_element_by_id('email').send_keys(Keys.BACK_SPACE) time.sleep(2) browser.find_element_by_id('email').send_keys('m') s(2) browser.find_element_by_id('email').send_keys(Keys.CONTROL,'a') s(2) browser.find_element_by_id('email').send_keys(Keys.CONTROL,'x')#剪切掉裏面的內容 s(2) browser.find_element_by_id('email').send_keys(Keys.CONTROL,'v') #從新輸入進去 s(2) browser.find_element_by_name('password').clear()#這個是以name選擇元素 browser.find_element_by_name('password').send_keys('password') #browser.find_element_by_xpath(".//*[@id='login']").click()#這個是以xpath選擇元素 browser.find_element_by_xpath(".//*[@id='login']").send_keys(Keys.ENTER) #這裏經過點擊Enter鍵來登陸 browser.maximize_window() article = browser.find_element_by_link_text(u'周碧華:社科院出現內鬼意味着什麼?') ActionChains(browser).move_to_element(article).perform()#將鼠標移動到這裏,可是這裏很差用 ActionChains(browser).context_click(article).perform() time.sleep(5) browser.quit() # 店鋪簽到https://www.v2ex.com/t/321539 def shop_check_in(self): urls = [ 'https://nongfutechan.taobao.com/ugo.htm?spm=a217e.7759359.15285.1.4xE9Il&signin=true#ugo-jinbi', 'https://shop136560499.taobao.com/ugo.htm?spm=a217e.7759359.15285.2.muhVBy&signin=true#ugo-jinbi', 'https://shop33473134.taobao.com/ugo.htm?spm=a217e.7759359.15285.3.yW2N4E&signin=true#ugo-jinbi' ] for url in urls: self.driver.get(url) time.sleep(5) print("page_source\t%s" % self.driver.page_source) self.driver.find_element_by_xpath('//a[@href="#" and @class="now-take J_NowSignIn" and text()="當即簽到"]').click() 增長 wait 時間,確保新頁面已經徹底加載 用 Google Chrome 的 XPath Helper 插件,確保本身寫的 XPath selector 無誤 打印源代碼 driver.page_source ,確認頁面內有「當即簽到」這個元素,而且 XPath selector 無誤
from http://www.yangyanxing.com/ar...php