Article From:https://www.cnblogs.com/yangjian319/p/9219058.html

 

webdriver

SeleniumIt is a set of Web automated testing tools developed by ThroughtWorks.
It is divided into three components: Selenium IDE, Selenium RC (Remote Control), Selenium Webdriver.
Selenium IDEIt is a plug-in of Firefox, allowing testers to record scripts and playback.
Selenium RCAnd Selenium Webdriver are testing frameworks that provide multilingual API. The difference is that Selenium Webdriver operates a browser in a more basic and flexible way, not just using JavaScript. So it can beIn order to bypass the browser’s Sandbox restrictions, Selenium RC does not support the framework, pop-up windows, page navigation, drop-down menus, AJAX based UI elements and other controls. Also, Selenium Webdriver does not need local servers.
Selenium 1.xThe version contains only the first two components. Webdriver is added from 2. The main problem is to solve the encryption problem of HTTPS.

 

WebdriverBefore you use, you need to download a driver plug-in for the browser. Please download it according to your different browser version.
GoogleI recommend to you a Baidu address:
Link: https://pan.baidu.com/s/1qZ2LfmW password: Qixa
After downloading, place chromdriver under the current directory of google.exe.

import os
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

chromedriver = "C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe"
#print(os.environ)
os.environ["webdriver.chrome.driver"] = chromedriver  # Add the path of the chromedriver.exe into the environment variable
driver = webdriver.Chrome(chromedriver)  # Start Google browsers
driver.get("http://www.python.org")  # Open the URL with a browser
driver.quit()

The results are as follows:

 

 Eight selectors

1. find_elements_by_name(“test”)
If there is a page as follows:

<input type="text" name="passwd" id="passwd-id" class="input-class"/>

Code:

element = driver.find_elements_by_name("passwd")

2. find_elements_by_id()

element = driver.find_element_by_id("passwd-id")

find_element_by_tagname

element = driver.find_element_by_tagname("input")

3. find_element_by_class_name(“input-class”)

4. find_elements_by_tag_name(“iframe”)

DOMTag elementsAn example of locating elements with Tag Name:<iframe src="..."></iframe>
element = driver.find_elements_by_tag_name("iframe")

5. find_element_by_link_text()
Page:

<a href="http://www.google.com/search?q=baidu">baidu</a>

Code:

element = browser.find_elements_by_link_text("baidu")

6.xpath This is particularly powerful. All elements can be found through this.

XPathIt is the abbreviation of XML Path, because HTML document itself is a standard XML page, so we can use XPath syntax to locate page elements.Absolute path: the root element begins, that is, HTML begins to use/ 
Relative road strength: an element of arbitrary conforming condition//
Find all the input elements on the page://input
Find the direct sub input element in the first form element on the page, that is, the next level input element that contains only the form element, which is represented by absolute path./://form[1]/input
Look up all the child input elements in the first form element on the page (as long as the input within the form element is counted, no matter how many other tags are nested, using the relative path, double//://form[1]//input
Find the first form element on the page://form[1]
Find the form element on the page that ID is loginForm://form[@id='loginForm']
Find the input element with name attribute username on the page://input[@name='username']
Find the first input element under the form element ID is loginForm on the page://form[@id='loginForm']/input[1]
The search page has input elements of name attribute contiune and type attribute button.//input[@name='continue'][@type='button']
Find the 4 input element of the form element ID is loginForm on the page://form[@id='loginForm']/input[4]

Control operation

Input box

element.clear()  Empty the input box dataElement.sendkeys ("username")   send dataElement.text gets the value of the text

Button

element.click()

Form submission

element.submit()

Single and multi selection box

element.clear()
element = browser.find_elements_by_id("checkbox")

Select a single option

element.click()

 

common method

browser.get_cookies()
browser.title       # Head name
browser.close() 
browser.forward()   # Forward
browser.back()      # Back off
browser.refresh()   # Refresh
browser.current_url # Return to the current page

 

Login 12306

from selenium import webdriver
import random
import time

def randomSleep(minS, maxS):
    time.sleep((maxS - minS) * random.random() + minS)
browser = webdriver.Chrome(r"C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe")  # chromedriver.exeStore path
url = "https://kyfw.12306.cn/otn/login/init"  # The URL to log in

browser.get(url=url)
print(browser.find_element_by_id("username").text)
browser.find_element_by_id("username").clear()  # Empty the input box data
browser.find_element_by_id("username").send_keys("974644081@qq.com")  # Send the user name to the input box
randomSleep(2, 5)
browser.find_element_by_id("password").send_keys("xxxxxxxxx")  # Send the password to the input box
randomSleep(1, 4)
time.sleep(6)
browser.find_element_by_id("loginSub").click()   # Click on the login button
randomSleep(2, 5)
print(browser.get_cookies())
browser.quit()

 

Log in to the Jingdong

from selenium import webdriver
import random
import time

def randomSleep(minS, maxS):
    time.sleep((maxS - minS) * random.random() + minS)
browser = webdriver.Chrome(r"C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe")  # chromedriver.exeStore path
# browser = webdriver.Chrome()

browser.get("https://passport.jd.com/new/login.aspx")  # The URL to log in

# Login by username and password
randomSleep(1, 2)
browser.find_element_by_xpath("//a[@clstag='pageclick|keycount|201607144|2']").click()
# browser.find_element_by_tag_name("pageclick|keycount|201607144|2").click()

# Username and password
randomSleep(1, 2)
browser.find_element_by_id("loginname").send_keys("xxxxxxx")  # Send the login username
print(browser.find_element_by_id("loginname").text)

randomSleep(1, 3)
browser.find_element_by_id("nloginpwd").send_keys("xxxxxx")  # Send the login password

# Submit, wait for a long time
randomSleep(5, 10)
browser.find_element_by_id("loginsubmit").click()  # Click on the login submission button
print(browser.get_cookies())
randomSleep(3, 5)
browser.quit()

 

Link of this Article: Python reptiles (three)

Leave a Reply

Your email address will not be published. Required fields are marked *