Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Selenium in Python

2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article is to share with you about how to use Selenium in Python. The editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.

Selenium introduction

Selenium is an automated testing tool for Web, originally developed for automated testing of websites. Selenium can run directly on browsers. It supports all major browsers (including PhantomJS, which has no interface (developers said in 2018 to suspend development, and chromedriver can achieve the same function). It can receive instructions and let browsers load pages automatically, get the data they need, and even take screenshots of pages.

Installation

Pip install selenium

Second, the introduction of Chromedriver

Chromedriver is also a browser that can be driven by selenium, but it is different from PhantomJS (there are a lot of this introduction on the Internet) in that it has an interface. Chromedriver download you need to first check the version of your Chrome browser, and then select the corresponding Chromedriver.

Download address: https://npm.taobao.org/mirrors/chromedriver

-chromedriver_win32.zip under Windows: version ChromeDriver v2.32 (2017-08-30) (supports Chrome v59-61) 1. Decompress chromedriver_win32.zip2. Move chromedriver.exe to the Python installation directory (actually you can put it anywhere, but it needs to be equipped with environment variables, so it is easy to put it in the Python installation directory) Note: there is a corresponding relationship between Chromedriver and the chrome version on your computer, it is recommended to use the latest Chromedriver version and update the chrome browser to the latest version III. Basic use of Selenium

Whether to turn on headless mode (that is, whether an interface is required)

From selenium.webdriver import Chromefrom selenium.webdriver.chrome.options import Optionsoption = Options () # instantiate option object option.add_argument ("--headless") # add headless parameter driver = Chrome to option object (executable_path=r "E:\ python Learning\ python crawler\ chromedriver.exe", # instantiate browser object, you can specify the path of chromedriver If it is not specified, it will go to the peer directory of the python interpreter options=option) # when instantiating the browser object, bring the option object in.

Save a screenshot of the current web page

From selenium.webdriver import Chromedriver = Chrome (executable_path=r "E:\ python Learning\ python Crawler\ chromedriver.exe", # instantiate the browser object and specify the path to chromedriver If not specified, it defaults to the peer directory of the python interpreter # options=option) # bring in the option object when instantiating the browser object) driver.get ("https://www.baidu.com/")driver.save_screenshot('baidu.png') # saves a screenshot of the current web page driver.close () # closes the current web page

Browser window maximization

From selenium.webdriver import Chromedriver = Chrome (executable_path=r "E:\ python Learning\ python Crawler\ chromedriver.exe") driver.maximize_window () # browser window maximization

Enter data in the input input box

From selenium.webdriver import Chromedriver = Chrome (executable_path=r "E:\ python Learning\ python Crawler\ chromedriver.exe") driver.maximize_window () # browser window maximization ele = driver.find_element_by_id ("kw") # find the node ele.send_keys whose id is kw ("Mathematics") # enter data into the input input box

Simulated click

From selenium.webdriver import Chromedriver = Chrome (executable_path=r "E:\ python Learning\ python Crawler\ chromedriver.exe") driver.maximize_window () # browser window maximization ele = driver.find_element_by_id ("kw") # find the node ele.send_keys whose id is kw ("Mathematics") # enter data into the input input box ele = driver.find_element_by_id ('su') # find id as Su node (Baidu) ele.click () # simulated click

Find nodes based on text values

# find the node driver.find_element_by_link_text whose text value is below Baidu ("Baidu") # get the list of elements according to the text contained in the link, and fuzzy match driver.find_elements_by_partial_link_text ("degree")

Gets the text of the current node

Ele.text # get the text ele.get_attribute ("data-click") of the current node # get the value corresponding to the attribute

Print some information about the current web page

Print (driver.page_source) # print the source code of the web page print (driver.get_cookies ()) # print the cookieprint (driver.current_url) of the web page # print the url of the current web page

Close the browser

Driver.close () # close the current web page driver.quit () # close the browser directly

Page waiting

From selenium.webdriver import Chromeimport timefrom selenium.webdriver.common.by import Byfrom selenium.webdriver.support.ui import WebDriverWaitfrom selenium.webdriver.support import expected_conditions as ECdriver = Chrome (executable_path=r "E:\ python Learning\ python Crawler\ chromedriver.exe") driver.get ("https://www.baidu.com/")# time.sleep (1) driver.implicitly_wait (5) # is similar to time.sleep and smarter than time.sleep No passive wait time ends wait = WebDriverWait (driver, 10) # wait time ele = wait.until (EC.presence_of_element_located ((By.ID, "kw")) ele.send_keys ("Mathematics") wait = WebDriverWait (driver, 10) ele = wait.until (EC.element_to_be_clickable ((By.ID, 'su')) ele.click () time.sleep (3) driver.close () IV, Selenium switch window

1. Get all current windows

Current_windows = driver.window_handles# returns a list.

two。 Switch according to the window index

Driver.switch_to.window (current_windows [0])

3. Selenium switch iframe

Driver.switch_to.frame ("id of iframe")

4. Switch to alert

Alert = driver.switch_to.alert () 5 Selenium sliding interface from selenium.webdriver import Chromeimport timedriver = Chrome (executable_path=r "E:\ python Learning\ python Crawler\ chromedriver.exe") driver.get ("https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=1&rsv_idx=1&tn=78000241_12_hao_pg&wd=selenium%20js%E6%BB%91%E5%8A%A8&fenlei=256&rsv_pq=8215ec3a00127601&rsv_t=a763fm%2F7SHtPeSVYKeWnxKwKBisdp%2FBe8pVsIapxTsrlUnas7%2F7Hoo6FnDp6WsslfyiRc3iKxP2s&rqlang=cn&rsv_enter=1&rsv_dl=tb&rsv_" Sug3=31&rsv_sug1=17&rsv_sug7=100&rsv_sug2=0&rsv_btype=i&inputT=9266&rsv_sug4=9770 ") # 1. Scroll to the bottom of the page js = "document.documentElement.scrollTop=800" # execute jsdriver.execute_script (js) time.sleep (1) # scroll to the top js = "document.documentElement.scrollTop=0" driver.execute_script (js) # advantages and disadvantages of executing jstime.sleep (1) driver.close () selenium

Selenium can execute the js on the page, and it is very easy to deal with js rendered data and simulated login.

Selenium is very inefficient because it sends a lot of requests during the process of getting the page, so it needs to be used as appropriate in many cases.

The above is how to use Selenium in Python. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report