Sometimes we look at the XPath information of the element with Firepath (not here) and find that there is no ID information that can be used to locate it, and we need to consider using the other available to locate the element. This article describes how to locate a page element through the value of class name in an element node. or Baidu home page, search input box positioning example:
XPath screenshot
The relevant scripting code is as follows:
# Coding=utf-8 from
Selenium import webdriver
driver = webdriver. Chrome ()
Driver.maximize_window ()
driver.implicitly_wait (6)
driver.get ("https://www.baidu.com")
Try:
driver.find_element_by_class_name ("S_ipt")
print (' Test pass:element found by class name ')
Except Exception as E:
print ("Exception found", Format (e))
driver.quit ()
Opinion: In many cases, class takes advantage of more than ID, and if there are too long characters in class and variable numbers, go back to the XPath positioning method.