Common usage of python reptile of selenium (a)

selenium is an automated testing tool, is simply simulate human operations to open the browser to turn the page, click, and other operations fell
installation process still pip install selenium
if pycharm can refer to this installation how pycharm install third-party libraries
for selenium in we use chrome browser, for example, we need a chromedriver drive before you can use, you can click on this URL to download the browser version of the driver corresponding here to download
can also CSDN download click here to download

The following describes the basic use start

from selenium import webdriver

browser = webdriver.Chrome(./driver/chromedriver.exe)
'''括号里的内容是驱动所在的目录,我们要指明他'''
browser.get("http://www.baidu.com")

'''运行一下我们会发现,自动打开了一个浏览器并且跳转到了百度首页'''

selenium will automatically help us jump to our designated URL, and subsequent operations
Here Insert Picture Description
we can find by grabbing the Baidu home page input box, enter the input box is a label, his id is kw, Ever

from selenium.webdriver.common.keys import Keys
'''我们要引入selenium的Keys模块,以便进行模拟'''

input_label = browser.find_element_by_id("kw")
'''通过方法名我们就可以看到他的作用就是通过id来找到元素,这里我们找到了输入框的位置'''
input_label.send_keys('CSDN')
'''我们往输入框的 里边输入关键字CSDN,哈哈,完全模拟人的操作啊'''
input_label.send_keys(Keys.ENTER)  # 这里要注意大小写啊
'''没错,这一步就是模拟回车动作,我们输入完关键字就回车'''

He can see automatically open the browser to jump to Baidu page, and enter the CSDN in the input box and press Enter to complete the inquiry, automation, we can print something to see

print(browser.current_url)
(结果)https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=1&rsv_idx=1&tn=baidu&wd=CSDN&rsv_pq=b73f3070003479d6&rsv_t=0cfdiO29N23GOG6K9sQTm2X3lEY14m2b0c3%2BlS0WYCN9Ml8xibJsbpyZn2c&rqlang=cn&rsv_enter=1&rsv_dl=tb&rsv_sug3=4&rsv_sug2=0&inputT=81&rsv_sug4=81
'''current_url  当前url,吧当前的url打印了出来'''

print(browser.get_cookies())
'''该方法打印出浏览器的cookie'''

print(browser.page_source)
'''打印当前页的源码啊'''

'''最后如果我们要关闭浏览器,可以用close()方法'''
browser.close()

A complete code follows the above-mentioned
Here Insert Picture Description
front we can say find_element_by_id(), we can actually be found to find the element you're looking for using the following method

print(browser.find_element_by_css_celecor("#kw"))
'''这样也开始可以查找到输入框的,和刚刚的通过id 查找效果一样'''
'''与此相类似的还有一下方法,都是可以查找的'''
find_element_by_name()
find_element_by_class_name()
find_element_by_tag_name()

'''以下的方式就是换了种写法,但是效果一样'''
find_element(By.ID, "#kw")
'''和find_element_by_id()的效果一样'''
..........

If we want to find more than one element of it? ? By id looking to die, after all, an id tag can only be a label, we can find more by class

print(browser.find_elements_by_css_celector(".xxx"))
'''举个例子演示一遍'''

We opened Taobao, open the developer mode
Here Insert Picture Description
following code

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

browser = webdriver.Chrome("./driver/chromedriver.exe")
browser.get(r"https://www.taobao.com")
res = browser.find_elements_by_css_selector(".J_Cat")
print(res)
(结果)
<selenium.webdriver.remote.webelement.WebElement (session="65727d99a5daa56eea23cc83f07b29a5", element="109c5b61-6ac9-4687-8f8d-e5e91863279d")>
<selenium.webdriver.remote.webelement.WebElement (session="65727d99a5daa56eea23cc83f07b29a5", element="de6b76ee-d7b8-4fb2-8bd4-dbeba24d5ecb")>
<selenium.webdriver.remote.webelement.WebElement (session="65727d99a5daa56eea23cc83f07b29a5", element="29454334-d91e-4d28-a811-a88d4a7a9ad0")>
<selenium.webdriver.remote.webelement.WebElement (session="65727d99a5daa56eea23cc83f07b29a5", element="70c5b0dc-8f52-4c45-a9ed-ad42c5336745")>
<selenium.webdriver.remote.webelement.WebElement (session="65727d99a5daa56eea23cc83f07b29a5", element="d6a4add0-8827-4331-9d00-376f40680e0f")>
<selenium.webdriver.remote.webelement.WebElement (session="65727d99a5daa56eea23cc83f07b29a5", element="f4fc481a-5707-4166-ac62-5d5bea5e7444")>
<selenium.webdriver.remote.webelement.WebElement (session="65727d99a5daa56eea23cc83f07b29a5", element="cbf5f2e2-bd8b-4cf3-831d-852d509a8e80")>
<selenium.webdriver.remote.webelement.WebElement (session="65727d99a5daa56eea23cc83f07b29a5", element="fe3061d6-ef05-4a91-a3b4-ee9b5ab983ff")>
<selenium.webdriver.remote.webelement.WebElement (session="65727d99a5daa56eea23cc83f07b29a5", element="f8293111-6097-4d72-9727-f5d0d04e4a10")>
<selenium.webdriver.remote.webelement.WebElement (session="65727d99a5daa56eea23cc83f07b29a5", element="38cfe91f-256a-4829-acd1-48e98dae7bbd")>
<selenium.webdriver.remote.webelement.WebElement (session="65727d99a5daa56eea23cc83f07b29a5", element="6cecfe0f-bb22-40a1-bd28-b2ec15ebac6c")>
<selenium.webdriver.remote.webelement.WebElement (session="65727d99a5daa56eea23cc83f07b29a5", element="91228eb8-db41-47bb-804c-546a461b5d97")>
<selenium.webdriver.remote.webelement.WebElement (session="65727d99a5daa56eea23cc83f07b29a5", element="52bf848a-6c05-4c8f-a2e1-8f5792306d2c")>
<selenium.webdriver.remote.webelement.WebElement (session="65727d99a5daa56eea23cc83f07b29a5", element="851bdcb0-4666-47df-9b1e-f65856c16562")>
<selenium.webdriver.remote.webelement.WebElement (session="65727d99a5daa56eea23cc83f07b29a5", element="a08608af-3c74-4b7f-8c35-095099e2ac7f")>
<selenium.webdriver.remote.webelement.WebElement (session="65727d99a5daa56eea23cc83f07b29a5", element="4aa4de96-d488-4df5-b558-eb87306f0efa")>

'''可以看到他打印了每一个标签,虽然看起来像乱麻,但是我们可以通过打印type来确定他的类型'''
'''对于其他的多选的方法,其实都是只要把element变成elements就好'''

These are the most basic of selenium operations, more complex operations please see the follow-up blog

Content Reference from python3 reptiles entry video

Published 62 original articles · won praise 20 · views 5804

Guess you like

Origin blog.csdn.net/weixin_44415928/article/details/104277121