import time import random from selenium import webdriver a=random.randint(1,3) if a==1: browser = webdriver.Chrome() elif a==2: browser=webdriver.Firefox() else: print(a) browser.get('http://www.baidu.com/') browser.implicitly_wait(2) browser.find_element_by_id('kw').send_keys("安全培训") browser.find_element_by_id("su").click() def fanye(): searchPage = browser.find_element_by_id("page") # sites = browser.find_elements_by_class_name('c-container') sites = browser.find_elements_by_xpath('//div[@id="content_left"]//h3') yeshu = searchPage.find_element_by_xpath('//div[@id="page"]//strong') print(yeshu.text) c=0 if int(yeshu.text) <= 4: for t in sites: # search_windows = browser.current_window_handle fwn = t.text fwn = fwn.strip() print(fwn) if fwn.find('安全管理网')!=-1: biaobiao=t.find_element_by_tag_name('a') biaobiao.click() break time.sleep(2) # time.sleep(1) c += 1 if c==len(sites): time.sleep(2) searchPage.find_element_by_xpath("//div[@id='page']//a[last()]").click() time.sleep(2) fanye() if __name__ == '__main__': fanye()
评论