Selenium of Python (automated browser testing)
1. Install selenium
pip install selenium -i https://pypi.tuna.tsinghua.edu.cn/simple
2. Download the corresponding version of the browser driver
http://npm.taobao.org/mirrors/chromedriver/
this is mine.
Put the decompressed driver in your own python.exe directory.
3. Test the code, open a webpage, and get the title of the webpage
from selenium.webdriver import Chrome
if __name__ == '__main__':
web = Chrome()
web.get("https://baidu.com")
print(web.title)
4. A small example
from selenium.webdriver import Chrome
if __name__ == '__main__':
web = Chrome()
url = 'https://ac.nowcoder.com/acm/home'
web.get(url)
# 获取要点击的a标签
el = web.find_element_by_xpath('/html/body/div/div[3]/div[1]/div[1]/div[1]/div/a')
# 点击
el.click() # "/html/body/div/div[3]/div[1]/div[2]/div[2]/div[2]/div[1]/h4/a"
# 爬取想要的内容
lists = web.find_elements_by_xpath("/html/body/div/div[3]/div[1]/div[2]/div[@class='platform-item js-item ']/div["
"2]/div[1]/h4/a")
print(len(lists))
for i in lists:
print(i.text)
5. Automatic input and jump
from selenium.webdriver import Chrome
from selenium.webdriver.common.keys import Keys
import time
if __name__ == '__main__':
web = Chrome()
url = 'https://ac.nowcoder.com/acm/home'
web.get(url)
el = web.find_element_by_xpath('/html/body/div/div[3]/div[1]/div[1]/div[1]/div/a')
el.click()
time.sleep(1)
input_el = web.find_element_by_xpath('/html/body/div/div[3]/div[1]/div[1]/div[1]/form/input[1]')
input_el.send_keys('牛客', Keys.ENTER)
# do something
That's all for this article, I hope it can help you, and finally : In order to give back to the die-hard fans, I have compiled a complete software testing video learning tutorial for you. If you need it, you can get it for free【保证100%免费】