Python Selenium XPath uses variables when positioning

Source: Internet
Author: User
Tags xpath

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.