Driver.find_element_by_xpath (input[@id = "kw"])
The above code, I believe a lot of learning Selenium + python friends are very familiar with, is to locate Baidu home search box code, if we want to "kw", with a variable to indicate how to operate it?
At present, I know there are two ways, such as the next, is to locate the Baidu search box, click the Search code, in the process of XPath positioning, using the variable:
#Encoding:utf-8 fromSeleniumImportWebdriverImportTimedriver=Webdriver. Firefox () Driver.get ("http://www.baidu.com")#The following paragraph uses the variable method in Python, which is simple! defEnter (Var): Driver.find_element_by_xpath ("//input[@id = '%s ']"% var). Send_keys ("haha") Enter ('kw') Time.sleep (3)#The following paragraph is used as if it is a fixed format, in JS seedefEnter1 (var1): Driver.find_element_by_xpath ("//input[@id = '"+ var1 +"']"). Click () enter1 ('su')
Time.sleep (3)
Driver.quit ()
Python Selenium XPath uses variables when positioning