See for installation
【python】Selenium+python3 Use summary ( One )
Realization function : Open the browser =》 Enter Baidu website to search =》 Enter in the search box python=》 Click Baidu
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
driver = webdriver.Chrome()
driver.get("https://www.baidu.com/")
driver.find_element(by=By.ID, value="kw").send_keys("python")
driver.find_element(by=By.ID, value="su").click()
driver.quit()
Element positioning is the core of automated testing
The positioning methods mainly include :(1)id ; (2) name; (3)class name ; (4) tag name ; (5) xpath ; (6) css selector ; (7) link test ; (8) partial link test ;
among xpath There are many positioning applications .
give an example :<input type="text" class="s_ipt" name="wd" id="kw" maxlength="100" autocomplete="off">
To locate this input Input box , There are several ways :
dri.find_element(by=By.ID, value=“kw”).send_keys(“python”)
dri.find_element(by=By.NAME, value=“wd”).send_keys(“python”)
dri.find_element(by=By.TAG_NAME, value=“kw”).send_keys(“python”)
dri.find_element(by=By.CLASS_NAME, value=“s_ipt”).send_keys(“python”)
dri.find_element(by=By.XPATH, value=“//input[@id=''kw]”).send_keys(“python”)
dri.find_element(by=By.CSS_SELECTOR, value=“#kw”).send_keys(“python”)
To locate this text link , There are several ways :
<a href="http://news.baidu.com" target="_blank" class="mnav"> Journalism </a>
dri.find_element(by=By.LINK_TEXT, value=“ Journalism ”).send_keys(“python”)
dri.find_element(by=By.PARTIAL_LINK_TEXT, value=“ Journalism ”).send_keys(“python”)
dri.maxmize_window()
dri.set_window_size(500,500)
dri.forward()
dri.back()
The common methods are as follows :
element.get_attribute(“value”)
ActionChains class :
You need to load from selenium.webdriver.common.action_chains import ActionChains
ActionChains(dri).context_click(ppp).perform() #ppp Is the element to be operated
1、 Mandatory waiting time time.sleep(3)
2、 Show waiting time WebDriverWait()
3、 Implicit waiting time implicitly_wait()
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
import time
driver = webdriver.Chrome()
element = WebDriverWait(driver, 10).until(lambda x: x.find_element(By.ID, "someId"))
is_disappeared = WebDriverWait(driver, 30, 1, (ElementNotVisibleException)).\
until_not(lambda x: x.find_element(By.ID, "someId").is_displayed())
driver.implicitly_wait(30)
time.sleep(3)