隐藏

python之selenium

发布:2023/12/21 23:58:39作者:管理员 来源:本站 浏览次数:690

1、安装

一、selenium


pip install selenium


二、驱动

(1)下载驱动


https://sites.google.com/a/chromium.org/chromedriver/downloads


https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/


https://github.com/mozilla/geckodriver/releases


https://webkit.org/blog/6900/webdriver-support-in-safari-10/


(2)配置环境


下载好对应版本驱动,配置环境PATH(Windows),PATH加入驱动路径css



2、简单实用

一、执行js,打开浏览器窗口


from selenium import webdriver

driver = webdriver.Chrome()

driver.get("http://8.8.8.8/")



for i in range(100):

js="window.open('http://1.1.1.%s')" % str(i)

driver.execute_script(js)


#driver.quit()



二、简单使用2


from selenium import webdriver

from selenium.webdriver.common.keys import Keys


driver = webdriver.Chrome()

driver.get("http://www.python.org")

assert "Python" in driver.title

elem = driver.find_element_by_name("q")

elem.clear() #清空自动填入的值,保证咱们的输入

elem.send_keys("pycon")

elem.send_keys(Keys.RETURN)

assert "No results found." not in driver.page_source

driver.close() #退出



三、获取元素


1.id定位:find_element_by_id(self, id_)

2.name定位:find_element_by_name(self, name)

3.class定位:find_element_by_class_name(self, name)

4.tag定位:find_element_by_tag_name(self, name)

5.link定位:find_element_by_link_text(self, link_text)

6.partial_link定位find_element_by_partial_link_text(self, link_text)

7.xpath定位:find_element_by_xpath(self, xpath)

8.css定位:find_element_by_css_selector(self, css_selector)

9.id复数定位find_elements_by_id(self, id_)

10.name复数定位find_elements_by_name(self, name)

11.class复数定位find_elements_by_class_name(self, name)

12.tag复数定位find_elements_by_tag_name(self, name)

13.link复数定位find_elements_by_link_text(self, text)

14.partial_link复数定位find_elements_by_partial_link_text(self, link_text)

15.xpath复数定位find_elements_by_xpath(self, xpath)

16.css复数定位find_elements_by_css_selector(self, css_selector




refer:https://www.cnblogs.com/yoyoketang/p/6557421.htmlhtml


https://selenium-python.readthedocs.io/locating-elements.htmlpython

四、一些方法和其余

(1)Selenium:利用select模块处理下拉框


from selenium.webdriver.support.select import Select


select_by_index # 经过索引定位git


select_by_value # 经过value值定位github


select_by_visible_text # 经过文本值定位web


根据索引选择 Select(driver.find_element_by_name("storeDeclare.cityLine")).select_by_index("3")chrome


根据value值选择 Select(driver.find_element_by_name("storeDeclare.cityLine")).select_by_value("3线")api


根据文本值选择 Select(driver.find_element_by_name("storeDeclare.cityLine")).select_by_visible_text("3线")浏览器


refer:cookie



(2)selenium中sendkeys()方法输入中文报错之解决方案


#方式1,在中文前加入u  # driver.find_element_by_class_name(“s_ipt”).send_keys(u’测试’)


#方式2,使用decode()方法

str = ‘测试’ print str driver.find_element_by_class_name(“s_ipt”).send_keys(str.decode(‘utf-8’))


(3)获取当前页面信息


记得延时,否则有可能出错


driver.title  #标题


driver.current_url  #连接



(4)输入


element.send_keys("some text")


(5)点击


driver.find_element_by_id("submit").click()



(6)前进后退


driver.forward()

driver.back()



(7)Cookies


cookie = {‘name’ : ‘foo’, ‘value’ : ‘bar’} driver.add_cookie(cookie) driver.get_cookies()


五、等待


https://selenium-python.readthedocs.io/waits.html

六、api


https://selenium-python.readthedocs.io/api.html


https://seleniumhq.github.io/selenium/docs/api/py/api.html