标签:final agg firefox windows ant 文本 因此 登录 query
from selenium import webdriver help(webdriver) # 查看用法以及支持范围
from selenium import webdriver browser = webdriver.Chrome() browser = webdriver.Firefox()
from selenium import webdriver PhantomJSdriver = r"D:\爬虫浏览器\phantomjs-2.1.1-windows\bin\phantomjs.exe" browser = webdriver.PhantomJS(PhantomJSdriver) browser.get("http://www.baidu.com") print(browser.page_source) browser.close() # 关闭浏览器
from selenium import webdriver PhantomJSdriver = r"D:\爬虫浏览器\phantomjs-2.1.1-windows\bin\phantomjs.exe" browser = webdriver.PhantomJS(PhantomJSdriver) browser.get("http://www.taobao.com") input_first = browser.find_element_by_id("q") input_second = browser.find_element_by_css_selector("#q") input_third = browser.find_element_by_xpath(‘//*[@id="q"]‘) print(input_first) print(input_second) print(input_third) browser.close()
from selenium import webdriver from selenium.webdriver.common.by import By PhantomJSdriver = r"D:\爬虫浏览器\phantomjs-2.1.1-windows\bin\phantomjs.exe" browser = webdriver.PhantomJS(PhantomJSdriver) browser.get("http://www.taobao.com") input_first = browser.find_element(By.ID, "q") print(input_first) browser.close()
from selenium import webdriver PhantomJSdriver = r"D:\爬虫浏览器\phantomjs-2.1.1-windows\bin\phantomjs.exe" browser = webdriver.PhantomJS(PhantomJSdriver) browser.get("http://www.taobao.com") lis = browser.find_elements_by_css_selector(‘.service-bd li‘) print(lis) browser.close()
from selenium import webdriver import py2exe import time # PhantomJSdriver = r"D:\爬虫浏览器\phantomjs-2.1.1-windows\bin\phantomjs.exe" # browser = webdriver.PhantomJS(PhantomJSdriver,service_args=["–ignore-ssl-errors=true", "–ssl-protocol=TLSv1"]) chromedriver = r"D:\chromedriver_win\chromedriver" # 正确版本的chromedriver browser = webdriver.Chrome(chromedriver) browser.get("http://www.taobao.com") input_str = browser.find_element_by_id(‘q‘) print("input_str", input_str) input_str.send_keys("ipad") time.sleep(1) input_str.clear() input_str.send_keys("MakBook pro") button = browser.find_element_by_class_name(‘btn-search‘) button.click()
from selenium import webdriver from selenium.webdriver import ActionChains # 鼠标 chromedriver = r"D:\chromedriver_win\chromedriver" # 正确版本的chromedriver browser = webdriver.Chrome(chromedriver) url = "http://www.runoob.com/try/try.php?filename=jqueryui-api-droppable" browser.get(url) browser.switch_to.frame(‘iframeResult‘) # 切换到iframeResult框架 source = browser.find_element_by_css_selector(‘#draggable‘) # 定位元素的原位置 target = browser.find_element_by_css_selector(‘#droppable‘) # 定位元素要移动到的目标位置 actions = ActionChains(browser) actions.drag_and_drop(source, target) # 拖动 actions.perform() # 执行拖动操作
from selenium import webdriver chromedriver = r"D:\chromedriver_win\chromedriver" # 正确版本的chromedriver browser = webdriver.Chrome(chromedriver) browser.get("http://www.zhihu.com/explore") browser.execute_script(‘window.scrollTo(0, document.body.scrollHeight)‘) browser.execute_script(‘alert("To Bottom")‘)
from selenium import webdriver chromedriver = r"D:\chromedriver_win\chromedriver" # 正确版本的chromedriver browser = webdriver.Chrome(chromedriver) url = ‘https://www.zhihu.com/explore‘ browser.get(url) logo = browser.find_element_by_id(‘zh-top-link-logo‘) print(logo) # 标签 print(logo.get_attribute(‘class‘))
from selenium import webdriver chromedriver = r"D:\chromedriver_win\chromedriver" # 正确版本的chromedriver browser = webdriver.Chrome(chromedriver) url = ‘https://www.zhihu.com/explore‘ browser.get(url) input = browser.find_element_by_class_name(‘zu-top-add-question‘) # 获取到标签 print(input.text)
from selenium import webdriver chromedriver = r"D:\chromedriver_win\chromedriver" # 正确版本的chromedriver browser = webdriver.Chrome(chromedriver) url = ‘https://www.zhihu.com/explore‘ browser.get(url) input = browser.find_element_by_class_name(‘zu-top-add-question‘) print(input.id) print(input.location) print(input.tag_name) print(input.size)
import time from selenium import webdriver from selenium.common.exceptions import NoSuchElementException chromedriver = r"D:\chromedriver_win\chromedriver" # 正确版本的chromedriver browser = webdriver.Chrome(chromedriver) url = ‘http://www.runoob.com/try/try.php?filename=jqueryui-api-droppable‘ browser.get(url) browser.switch_to.frame(‘iframeResult‘) source = browser.find_element_by_css_selector(‘#draggable‘) print(source) try: logo = browser.find_element_by_class_name(‘logo‘) except NoSuchElementException: print(‘NO LOGO‘) browser.switch_to.parent_frame() logo = browser.find_element_by_class_name(‘logo‘) print(logo) print(logo.text)
from selenium import webdriver chromedriver = r"D:\chromedriver_win\chromedriver" # 正确版本的chromedriver browser = webdriver.Chrome(chromedriver) browser.implicitly_wait(10) # 加载好了就不用等 browser.get(‘https://www.zhihu.com/explore‘) input = browser.find_element_by_class_name(‘zu-top-add-question‘) print(input)
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC chromedriver = r"D:\chromedriver_win\chromedriver" # 正确版本的chromedriver browser = webdriver.Chrome(chromedriver) browser.get(‘https://www.taobao.com/‘) wait = WebDriverWait(browser, 10) # 如果条件不成立就一直等10s input = wait.until(EC.presence_of_element_located((By.ID, ‘q‘))) button = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, ‘.btn-search‘))) print(input, button)
import time from selenium import webdriver chromedriver = r"D:\chromedriver_win\chromedriver" # 正确版本的chromedriver browser = webdriver.Chrome(chromedriver) browser.get(‘https://www.baidu.com/‘) # 第一页 browser.get(‘https://www.taobao.com/‘) # 第二页 browser.get(‘https://www.python.org/‘) # 第三页 browser.back() time.sleep(1) browser.forward() browser.close()
from selenium import webdriver chromedriver = r"D:\chromedriver_win\chromedriver" # 正确版本的chromedriver browser = webdriver.Chrome(chromedriver) browser.get(‘https://www.zhihu.com/explore‘) print(browser.get_cookies()) browser.add_cookie({‘name‘: ‘name‘, ‘domain‘: ‘www.zhihu.com‘, ‘value‘: ‘zhaofan‘}) print(browser.get_cookies()) browser.delete_all_cookies() print(browser.get_cookies())
import time from selenium import webdriver # 切换选项卡操作(页面选项卡) chromedriver = r"D:\chromedriver_win\chromedriver" # 正确版本的chromedriver browser = webdriver.Chrome(chromedriver) browser.get(‘https://www.baidu.com‘) browser.execute_script(‘window.open()‘) print(browser.window_handles) browser.switch_to_window(browser.window_handles[1]) browser.get(‘https://www.taobao.com‘) time.sleep(1) browser.switch_to_window(browser.window_handles[0]) browser.get(‘https://python.org‘)
from selenium import webdriver from selenium.common.exceptions import TimeoutException, NoSuchElementException chromedriver = r"D:\chromedriver_win\chromedriver" # 正确版本的chromedriver browser = webdriver.Chrome(chromedriver) try: browser.get(‘https://www.baidu.com‘) except TimeoutException: print(‘Time Out‘) try: browser.find_element_by_id(‘hello‘) except NoSuchElementException: print(‘No Element‘) finally: browser.close()
标签:final agg firefox windows ant 文本 因此 登录 query
原文地址:https://www.cnblogs.com/cjj-zyj/p/10045126.html