python爬虫 切换窗口与休眠
#切换到新窗口
import time from selenium import webdriver from selenium.webdriver.firefox.options import Options as FOptions options=FOptions() browser=webdriver.Firefox(executable_path="/Users/mac126/geckodriver",firefox_options=options)
# browser.get('http://www.baidu.com') # handles=browser.window_handles # browser.switch_to.window(handles[-1])#切换到最后打开的窗口 # browser.switch_to.window(handles[0])#切回原来窗口 #执行javascript # browser.get('https://sou.zhaopin.com/?jl=719&kw=Python&kt=3') # time.sleep(3)#强制休眠 # browser.execute_script('window.scrollTo(0,document.body.scrollHeight)') # # time.sleep(10) # # browser.close() #隐式休眠 #显示休眠 #隐式休眠是全局的,如果找不到等待设置的秒数,再次找不到抛出异常 browser.implicitly_wait(2) url='http://www.taobao.com' browser.get(url) wait=WebDriverWait(browser,10)#wait对象可以多个,指明达成条件的休眠的时间 wait2=WebDriverWait(browser,2) # input1=browser.find_element_by_id('q') input1=wait.until(EC.presence_of_element_located((By.ID,"q")))#等待,知道ID到q的元素出现为止,超时时间10秒 button=wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,".btn-search")))#等待,知道class是btn-search的元素可点击为止 print(input1,button) browser.close()