preface

Let’s take a look at Selenium’s definition:

Selenium is a tool for Web application testing. Selenium tests run directly in the browser, just as real users do. Supported browsers include Internet Explorer (7, 8, 9, 10, 11), Mozilla Firefox, Safari, Google Chrome, Opera, and more. The main functions of this tool include: testing browser compatibility – testing your application to see if it works well on different browsers and operating systems. Test system functionality – Create regression tests to verify software functionality and user requirements. Supports automatic recording of actions and automatic generation of test scripts in.net, Java, Perl and other languages.

Selenium is installed

1. The PIP installation:

Enter the following code under the PIP path:


pip install selenium

Copy the code

2. Pycharm install:

Pycharm: File–>Default Settings

Firefox Environment Configuration

1. Download Firefox from the Internet. Easy, no more talking

2. Add environment variables to the Firefox path

Find the path to Firefox, such as C:\Program Files\Mozilla Firefox, and add:

**3. Download geckoDriver, ** Download url:

https://github.com/mozilla/geckodriver/releases

Put geckodriver.exe in the Firefox installation path after downloading (don’t forget)

4. Download Selenium IDE in Firefox

Then click: Get more components, enter Selenium IDE, download and install.

5. Verify

You can enter in the command window:


firefox.exe

Copy the code

See if you can jump to the page.

You can also type in the Python editor:


from selenium import webdriver
browser = webdriver.Firefox()
browser.get('http://baidu.com')

Copy the code

If successful, the jump is normal.


Python-selenium: Elements drag and drop, page switching, etc.

Selenium installation is described above

The selenium API can be used directly in the following ways:

Single element selection

  • find_element_by_id

  • find_element_by_name

  • find_element_by_xpath

  • find_element_by_link_text

  • find_element_by_partial_link_text

  • find_element_by_tag_name

  • find_element_by_class_name

  • find_element_by_css_selector

Multiple element selection

  • find_elements_by_name

  • find_elements_by_xpath

  • find_elements_by_link_text

  • find_elements_by_partial_link_text

  • find_elements_by_tag_name

  • find_elements_by_class_name

  • find_elements_by_css_selector

Selenium Selenium

1. Page interaction

As an example, open the Baidu browser, enter “Selenium”, search and print the source code as follows:


def pageInteraction():
   driver = webdriver.Firefox()
   driver.get('http://www.baidu.com')
   Wait implicitly, in order to wait for the url to fully load
   driver.implicitly_wait(5)
   write = driver.find_element_by_id("kw")
   write.send_keys("Selenium")
   # click
   driver.find_element_by_id('su').click()
   try:
       Scan the page every 0.5 ms for 5 seconds until the specified element changes
       wait = WebDriverWait(driver, 5)
       wait.until(lambda driver: driver.find_element_by_id("content_left"))
       # print the source code
       print(driver.page_source)

   except TimeoutException:
       print("Query element timed out")
   finally:
       driver.close()

Copy the code

2. Push and drop page elements

Need to import ActionChains package, URL from the network


def elementDragging():
   try:
       driver = webdriver.Firefox()
       url = "http://www.runoob.com/try/try.php?filename=jqueryui-api-droppable"
       driver.get(url)
       driver.implicitly_wait(5)
       Switch to the frame of the element
       driver.switch_to.frame("iframeResult")
       The starting point #
       start = driver.find_element_by_id("draggable")
       # end
       end = driver.find_element_by_id("droppable")

       actions = ActionChains(driver)
       actions.drag_and_drop(start, end)
       # to perform
       actions.perform()
   except Exception:
       print("exception")
   finally:
       driver.close()

Copy the code

3. Switch pages

Search for keywords through Baidu and open a web page, switch to a new window page, and then open a third page

The following three methods are mainly used:

  • Current_window_handl: Gets the handle to the current window

  • Window_handles: Returns all window handles to the current session

  • Switch_to_window () : Switches the window function


def pageSwitching():
   driver = webdriver.Firefox()
   driver.get('http://www.baidu.com')
   Get the current Baidu window handle
   BD_windows = driver.current_window_handle
   # print
   print(BD_windows)
   Wait implicitly, in order to wait for the url to fully load
   driver.implicitly_wait(5)

   write = driver.find_element_by_id("kw")
   write.send_keys("CSDN")
   # click
   driver.find_element_by_id('su').click()
   try:
       Open a web page
       driver.find_element_by_link_text(u'CSDN- Professional IT Community ').click()
       Wait implicitly, in order to wait for the url to fully load
       driver.implicitly_wait(5)
       Print all Windows
       print(driver.window_handles)
       Wait implicitly, in order to wait for the url to fully load
       driver.implicitly_wait(5)
       # Window switches to the second page
       driver.switch_to_window(driver.window_handles[1])
       Click the "Blog" button on the second page
       driver.find_element_by_link_text(u'Blog').click()
       time.sleep(5)

   except Exception:
       print("exception")
   finally:
       driver.quit()

Copy the code

4. Popover processing


alert = driver.switch_to_alert()
print(alert .text)
alert .accept()

Copy the code

Here is the full source code:


# coding=utf-8
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver import ActionChains
import time

# Page interaction
def pageInteraction():
   driver = webdriver.Firefox()
   driver.get('http://www.baidu.com')
   Wait implicitly, in order to wait for the url to fully load
   driver.implicitly_wait(5)
   write = driver.find_element_by_id("kw")
   write.send_keys("Selenium")
   # click
   driver.find_element_by_id('su').click()
   try:
       Scan the page every 0.5 ms for 5 seconds until the specified element changes
       wait = WebDriverWait(driver, 5)
       wait.until(lambda driver: driver.find_element_by_id("content_left"))
       # print the source code
       print(driver.page_source)

   except TimeoutException:
       print("Query element timed out")
   finally:
       time.sleep(3)
       driver.close()

# page element drag and drop
def elementDragging():
   try:
       driver = webdriver.Firefox()
       url = "http://www.runoob.com/try/try.php?filename=jqueryui-api-droppable"
       driver.get(url)
       driver.implicitly_wait(5)
       Switch to the frame of the element
       driver.switch_to.frame("iframeResult")
       The starting point #
       start = driver.find_element_by_id("draggable")
       # end
       end = driver.find_element_by_id("droppable")

       actions = ActionChains(driver)
       actions.drag_and_drop(start, end)
       # to perform
       actions.perform()
   except Exception:
       print("exception")
   finally:
       driver.close()

# Page switch
def pageSwitching():
   driver = webdriver.Firefox()
   driver.get('http://www.baidu.com')
   Get the current Baidu window handle
   BD_windows = driver.current_window_handle
   # print
   print(BD_windows)
   Wait implicitly, in order to wait for the url to fully load
   driver.implicitly_wait(5)

   write = driver.find_element_by_id("kw")
   write.send_keys("CSDN")
   # click
   driver.find_element_by_id('su').click()
   try:
       Open a web page
       driver.find_element_by_link_text(u'CSDN- Professional IT Community ').click()
       Wait implicitly, in order to wait for the url to fully load
       driver.implicitly_wait(5)
       Print all Windows
       print(driver.window_handles)
       Wait implicitly, in order to wait for the url to fully load
       driver.implicitly_wait(5)
       # Window switches to the second page
       driver.switch_to_window(driver.window_handles[1])
       Click the "Blog" button on the second page
       driver.find_element_by_link_text(u'Blog').click()
       time.sleep(5)

   except Exception:
       print("exception")
   finally:

       driver.quit()

if __name__ == '__main__':
    pageInteraction()
    #pageSwitching()
    #elementDragging()

Copy the code

You can pay attention to my wechat public account: “Qin Zishuai” a quality, attitude of the public account!