1. Problems
1. Selenium 3.0+, Google Chrome 6.0+, added chromedriver.exe running error: "unsafe: data" and so on:
The selenium version is inconsistent with the google version, it is estimated that the Google version is too high; this problem has not occurred in the Firefox browser for the time being, and geckodriver.exe should also be downloaded
2. After jumping from a page to a new page, the new element cannot be located, or the new page element is not stored:
After going to the new page, convert the original driver before using it: driver.switch_to_window(driver.window_handles[-1]
"-1" means the last page, that is, the latest meaning, you can locate the page that has been opened according to handles
2. Example code
1. Simple open and input box input jump
from selenium import webdriver
import time
# Open the browser url interface
firefox = webdriver.Firefox()
url = 'http://www.jd.com/'
firefox.get(url)
time.sleep(1)
from selenium.webdriver.common.keys import Keys #Simulate keyboard operation module
#Locate to the input box element and automatically enter the content, simulating the keyboard jump
jd = firefox.find_element_by_id('key')jd.clear()
jd.send_keys("python")
jd.send_keys(Keys.ENTER) # Simulate the keyboard "Enter" key
2. Simulate login instance + locate new page elements
import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
def login(url, keyword):
jdlogin = webdriver.Firefox()
jdlogin.get(url)
time.sleep(1)
# There are two types of login on JD.com: QR code and account login. The following sentence starts with account login method.
time.sleep(1)
# Enter the account name
jdname = jdlogin.find_element_by_id('loginname')
jdname.clear() #Clear the original content in the input box
jdname.send_keys("***********")
# Enter the password
jdpwd = jdlogin.find_element_by_id( 'nloginpwd')
jdpwd.send_keys("**********")
jdpwd.send_keys(Keys.ENTER) # Simulate keyboard "Enter"
time.sleep(1)
# Call the following function
get_main_page(jdlogin, keyword)
def get_main_page(firefox, keyword):
firefox.switch_to_window(firefox.window_handles[-1]) #Jump the focus to the current page (the page after login)jd = firefox.find_element_by_id('key')
jd.clear() #Clear the original content in the input box
jd .send_keys(keyword)
jd.send_keys(Keys.ENTER)
url = firefox.current_url # Get the url of the current page
# Call the following function
get_goods(firefox)
def get_goods(firefox):
firefox.switch_to_window(firefox.window_handles[-1] ) # Jump focus to the current page
goods = firefox.find_elements_by_class_name('gl-item')
print(goods)
if __name__ == '__main__': # Main function
url = 'https://passport.jd.com/uc/login?ltype=logout'
login(url, "python")
3、find_element_by_css_selector用法
driver.find_element_by_css_selector()
CSS positioning can be divided into four categories: id , class , other attributes, and paths.
3.1 #id method
There are two ways, you can add the tag name in front, or you can not add
driver.find_element_by_css_selector(‘#id_value’)
driver.find_element_by_css_selector(‘tag_name#id_value’)
3.2 .class method
There are two ways, the tag name is added in front, or it can be omitted. If the tag name is not added, the dot cannot be omitted.
driver.find_element_by_css_selector(‘.class_value’)
driver.find_element_by_css_selector(‘tag_name.class_value’)
If the class also contains other tags such as <em></em>, <a> </a> can be directly indexed ('.class_value em')
driver.find_element_by_css_selector(‘.class_value em’)
Some class_values are relatively long, and when there is a space in the middle, the space cannot be written in as it is, otherwise it will not be recognized. In this case, spaces are replaced with dots, preceded by tag_name .
driver.find_element_by_css_selector('div.panel.panel-email').click()
3.3 Other property methods
There are two ways, you can add the tag name in front, or you can not add it.
driver.find_element_by_css_selector(“[attri_name=’attri_value’]”)
driver.find_element_by_css_selector("input[type='password']").send_keys(' I want to learn the net by myself ')
driver.find_element_by_css_selector("[type='password']").send_keys(' I want to learn by myself ')
3.4 Path method
There are two ways, you can add the tag name in front, or you can not add it. Note that its hierarchy uses the greater-than sign " > ".
driver.find_element_by_css_selector("form#loginForm>ul>input[type='password']").send_keys('111222333')