In the previous series, we introduced the following 8 methods to locate elements.
- find_element_by_id: locate the element through the ID attribute value of the element;
- find_element_by_name: locate an element by its name attribute value;
- find_element_by_class_name: locate the element by its class attribute value;
- find_element_by_xpath: locate elements through Xpath;
- find_element_by_tag_name: locate an element by its tag name;
- find_element_by_css_selector: locate elements through CSS selectors;
- find_element_by_link_text: locate the element through the text information between the element label pairs;
- find_element_by_partial_link_text: locate the element by some text information between the element label pairs.
WebDriver also provides another method, find_element(), which declares the positioning method through By and passes in the positioning parameters of the corresponding positioning method. find_ The element () method is only used to locate elements. It needs to pass in two parameters. The first parameter is the location type, which is provided By by (before use, you need to import By through from selenium.webdriver.common.by import By). The second parameter is the specific location method.
Taking the search button element as an example, the html code of the search button element is as follows:
<input type="submit" class="b_searchboxSubmit" id="sb_form_go" tabindex="0" name="go">
Corresponding find_ The method of element() is as follows:
- find_element(By.ID, "sb_form_go");
- find_element(By.CLASS_NAME,"b_searchboxSubmit");
- find_element(By.NAME,"go");
- find_element(By.CSS_SELECTOR,".b_searchboxSubmit");
- find_element(By.XPATH,"//*[@id='sb_form_go']");
- find_element(By.LINK_TEXT, "") because the element of the search box has no Link text, the attribute value is used instead;
- find_element(By.PARTIAL_LINK_TEXT,"****");
- find_element(By.TAG_NAME,"****") .
Take the Bing search page as an example.
- Find the search box and search button elements;
- Type the bella keyword in the search box;
- Click the search button with the mouse;
- Submit a search request.
By The complete code of ID is as follows:
from selenium import webdriver from time import sleep from selenium.webdriver.common.by import By driver = webdriver.Firefox() driver.get("http://cn.bing.com/") driver.find_element(By.ID,value="sb_form_q").send_keys("bella") driver.find_element(By.ID,value="sb_form_go").click() sleep(1) driver.quit()
By The complete code of name is as follows:
from selenium import webdriver from time import sleep from selenium.webdriver.common.by import By driver = webdriver.Firefox() driver.get("http://cn.bing.com/") driver.find_element(By.NAME,"q").send_keys("bella") driver.find_element(By.NAME,"go").click() sleep(1) driver.quit()
By CLASS_ The complete code of name is as follows:
from selenium import webdriver from time import sleep from selenium.webdriver.common.by import By driver = webdriver.Firefox() driver.get("http://cn.bing.com/") driver.find_element(By.CLASS_NAME,value="b_searchbox").send_keys("bella") driver.find_element(By.CLASS_NAME,value="b_searchboxSubmit").click() sleep(1) driver.quit()
By The complete code of XPath is as follows:
from selenium import webdriver from time import sleep from selenium.webdriver.common.by import By driver = webdriver.Firefox() driver.get("http://cn.bing.com/") driver.find_element(By.XPATH,"//*[@id='sb_form_q']").send_keys("bella") driver.find_element(By.XPATH,"//*[@id='sb_form_go']").click() sleep(1) driver.quit()
By CSS_ The complete code of the selector is as follows:
from selenium import webdriver from time import sleep from selenium.webdriver.common.by import By driver = webdriver.Firefox() driver.get("http://cn.bing.com/") driver.find_element(By.CSS_SELECTOR,".b_searchbox").send_keys("bella") driver.find_element(By.CSS_SELECTOR,".b_searchboxSubmit").click() sleep(1) driver.quit()
If you don't enjoy reading the article, you can check out the detailed video tutorial.
[2021] UI automation test: Selenium 3 automation test
(https://edu.csdn.net/course/detail/29953)
[test a full series of video courses] please click me
(https://edu.csdn.net/agency/59)
Title books are available in Jingdong and Dangdang
JD.COM: https://item.jd.com/12784287.html
Dangdang: http://product.dangdang.com/29177828.html