Selenium使用流程:1.环境安装:pipinstallselenium2.下载一个浏览器驱动(GoogleChrome)3.实例化一个浏览器对象\Application\chromedriver.exe")bro.get(url='http://scxk.nmpa.gov.cn:81/xk/')page_text=bro.page_sourcetree=etree.HTML(page_text)li_list=tree.xpath('//*[@id="gzlist"]/li')forliinli_list:name=li.xpath('./dl/@title')[0]print(name)sleep(5)bro.quit()基于浏览器自动化的操作代码#编写基于浏览器自动化的操作代码发起请求:get(url)标签定位:查找系列方法标签交互:send_keys('xxx')执行js程序:execute_script('jsCod')forward,向后:back(),forward()关闭浏览器:quit()1代码https://www.taobao.com/fromseleniumimportwebdriverfromtimeimportsleepbro=webdriver.Chrome(executable_path=r"E:\google\Chrome\Application\chromedriver.exe")bro.get(url='https://www.taobao.com/')#labellocationsearch_input=bro.find_element_by_id('q')sleep(2)#执行一组js代码使滚动滚轮向下滑动bro.execute_script('window.scrollTo(0,document.body.scrollHeight)')sleep(2)#labelinteractionsearch_input.send_keys('women')button=bro.find_element_by_class_name('btn-search')button.click()bro.get('https://www.baidu.com')sleep(2)bro.back()sleep(2)bro.forward()sleep(5)bro.quit()selenium处理iframe:如果iframe标签中存在定位标签,则必须使用switch_to.frame(id)动作链(拖动):fromselenium.webdriverimportActionChains实例化一个动作链对象:action=ActionChains(bro)click_and_hold(div):长按点击操作move_by_offset(x,y)perform()让动作链立即执行action.release()释放动作链对象代码https://www.runoob.com/try/try.php?filename=jqueryui-api-droppablefromseleniumimportwebdriverfromtimeimportsleepfromselenium.webdriverimportActionChainsbro=webdriver.Chrome(executable_path=r"E:\google\Chrome\Application\chromedriver.exe")bro.get('https://www.runoob.com/try/try.php?filename=jqueryui-api-droppable')bro.switch_to.frame('我frameResult')div=bro.find_element_by_id('draggable')#actionchainaction=ActionChains(bro)action.click_and_hold(div)foriinrange(5):action.move_by_offset(17,0).perform()sleep(0.3)#释放动作链action.release()bro.quit()selenium模拟登录QQ区码https://qzone.qq.com/fromseleniumimportwebdriverfromtimeimportsleepbro=webdriver.Chrome(executable_path=r"E:\google\Chrome\Application\chromedriver.exe")bro.get('https://qzone.qq.com/')bro.switch_to.frame("login_frame")switcher=bro.find_element_by_id('switcher_plogin')switcher.click()user_tag=bro.find_element_by_id('u')password_tag=bro.find_element_by_id('p')user_tag.send_keys('1234455')password_tag.send_keys('qwer123')sleep(1)but=bro.find_element_by_id('login_button')but.click()无头浏览器和逃避检测代码fromseleniumimportwebdriverfromtimeimportsleep#realizenovisualinterfacefromselenium.webdriver.chrome.optionsimportOptions#realizeevasiondetectionfromselenium.webdriverimportChromeOptions#realizeNo可视界面chrome_options=Options()chrome_options.add_argument('--headless')chrome_options.add_argument('--disable-gpu')#实现逃避检测option=ChromeOptions()option.add_experimental_option('excludeSwitches',['enable-automation'])bro=webdriver.Chrome(executable_path=r"E:\google\Chrome\Application\chromedriver.exe",chrome_options=chrome_options,options=option)bro.get('https://www.baidu.com')print(bro.page_source)sleep(2)bro.quit()【小编??推荐】微服务面试必问的Dubbo,写的这么详细我怕是不会找工作?2021年值得关注的5大IT行业发展趋势免费安全软件是孤独的!尴尬的是界面UI即将大改!Windows1021H2最新预览版抢先看微软为Windows101909推送KB5000850更新,修复资源管理器搜索等问题
