赞
踩
https://www.selenium.dev/
https://www.selenium.dev/documentation/en/webdriver/driver_requirements/
https://npm.taobao.org/mirrors/chromedriver/
https://chromedriver.storage.googleapis.com/index.html
https://github.com/SeleniumHQ/selenium-ide/releases
以上都无法下载
看这儿:https://www.likecs.com/show-205155234.html
小白入门:http://selenium-python.readthedocs.io/
from selenium import webdriver from selenium.webdriver.common.by import By from time import sleep class TestBaidu: def setup(self): self.driver = webdriver.Chrome() self.driver.maximize_window() # 隐式等待 类似于异步 self.driver.implicitly_wait(5) def teardown(self): # 退出所有标签 self.driver.quit() def test_baidu(self): self.driver.get('http://www.baidu.com') self.driver.find_element(by=By.ID,value='kw').send_keys('爱玛科技') self.driver.find_element(by=By.ID,value='su').click() self.driver.find_element(by=By.LINK_TEXT,value='爱玛科技[603529]A股实时行情 - 百度股市通').click() sleep(2)
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions from selenium.webdriver.support.wait import WebDriverWait class Test_wait: def setup(self): self.driver = webdriver.Chrome() self.driver.maximize_window() self.driver.get('https://www.jd.com/?cu=true&utm_source=baidu-pinzhuan&utm_medium=cpc&utm_campaign=t_288551095_baidupinzhuan&utm_term=0f3d30c8dba7459bb52f2eb5eba8ac7d_0_579fb540a49b4b5fbb41cb802ab375c1') def test_wait(self): self.driver.find_element(by=By.LINK_TEXT, value='京东家电').click() # 一点要穿一个参数进去 无论有没有使用 # def wait(x): # return len(self.driver.find_element(by=By.CLASS_NAME, value='lc-imgarea-base__links-item')) >= 4 # WebDriverWait(self.driver,8).until(wait) WebDriverWait(self.driver,6).until(expected_conditions.element_to_be_clickable([By.CSS_SELECTOR,'.logo'])) self.driver.find_element(by=By.LINK_TEXT, value='企业购').click()
selenium点击与输入
import pytest from selenium import webdriver from selenium.webdriver import ActionChains, Keys from selenium.webdriver.common.by import By from time import sleep class TestAction: def setup(self): self.driver = webdriver.Chrome() self.driver.implicitly_wait(3) self.driver.maximize_window() def teardown(self): self.driver.quit() print('结束了!') def test_case_click(self): self.driver.get("http://sahitest.com/demo/clicks.htm") element_click = self.driver.find_element(by=By.XPATH,value="//input[@value='click me']") element_dbl = self.driver.find_element(by=By.XPATH,value="//input[@value='dbl click me']") element_rcm = self.driver.find_element(by=By.XPATH,value="//input[@value='right click me']") action = ActionChains(self.driver) action.click(element_click) action.double_click(element_dbl) action.context_click(element_rcm) action.perform() sleep(3) # @pytest.mark.skip def test_case_move(self): self.driver.get('http://www.baidu.com') ele = self.driver.find_element(by=By.ID,value="s-usersetting-top") action = ActionChains(self.driver) action.move_to_element(ele).perform() sleep(3) def test_case_dragdrop(self): self.driver.get('http://sahitest.com/demo/dragDropMooTools.htm') drag_element = self.driver.find_element(by=By.XPATH,value='//*[@id="dragger"]') drop_element = self.driver.find_element(by=By.XPATH,value='/html//div[3]') drop_element2 = self.driver.find_element(by=By.XPATH, value='/html//div[5]') drop_element3 = self.driver.find_element(by=By.XPATH, value='/html//div[2]') action = ActionChains(self.driver) action.drag_and_drop(drag_element,drop_element).perform() action.click_and_hold(drag_element).release(drop_element2).perform() action.click_and_hold(drag_element).move_to_element(drop_element3).release().perform() sleep(3) def test_case_keys(self): self.driver.get('http://sahitest.com/demo/label.htm') ele = self.driver.find_element(by=By.XPATH,value='/html/body/label[1]/input') ele.click() action = ActionChains(self.driver) action.send_keys("Liang") action.send_keys(Keys.SPACE).pause(1) action.send_keys("XiaoQii").pause(1) action.send_keys(Keys.BACKSPACE).perform() sleep(3) if __name__ == '__main__': pytest.main()
https://www.selenium.dev/selenium/docs/api/py/webdriver/selenium.webdriver.common.touch_action.html
from selenium import webdriver # 现在没有这个TouchActions了 from selenium.webdriver import TouchActions from selenium.webdriver.common.by import By class touchAction: def setup(self): option = webdriver.ChromeOptions() option.add_experimental_option('w3c', False) self.driver = webdriver.Chrome(options=option) self.driver.implicitly_wait(3) self.driver.maximize_window() def teardown(self): self.driver.quit() print('结束了!') def test_TouchAction(self): self.driver.get('http://www.baidu.com') kw = self.driver.find_element(by=By.XPATH,value='//*[@id="kw"]') su = self.driver.find_element(by=By.XPATH,value='//*[@id="su"]') kw.send_keys('TouchAction') action = TouchActions(self.driver) action.tap(su) action.perform() action.scroll_from_element(kw,0,10000).perform()
但是这个呢 已经用不上了
from selenium_base import Base from selenium.webdriver import ActionChains from time import sleep class Test_switch(Base): def test_switch(self): self.driver.get('http://www.baidu.com') self.driver.find_element(by=self.By.XPATH,value='//*[@id="s-top-loginbtn"]').click() register_btn = self.driver.find_element(by=self.By.XPATH,value='//*[@id="TANGRAM__PSP_11__regLink"]') action = ActionChains(self.driver) action.click(register_btn) action.perform() print(self.driver.current_window_handle,self.driver.window_handles) window_handles = self.driver.window_handles self.driver.switch_to.window(window_handles[-1]) print(self.driver.current_window_handle) self.driver.find_element(by=self.By.ID,value='TANGRAM__PSP_4__userName').send_keys("LiangAnqi") sleep(0.5) self.driver.switch_to.window(window_handles[0]) sleep(5)
https://www.runoob.com/try/try.php?filename=jqueryui-api-droppable
from selenium_base import Base
class Test_Switch(Base):
def test_switch(self):
self.driver.get('https://www.runoob.com/try/try.php?filename=jqueryui-api-droppable')
self.driver.switch_to.frame('iframeResult')
drag = self.driver.find_element(by=self.By.ID,value="draggable")
drop = self.driver.find_element(by=self.By.ID,value="droppable")
print(drag.text,drop.text)
self.driver.switch_to.default_content()
print(self.driver.find_element(by=self.By.XPATH,value='//*[@id="submitBTN"]').text)
self.sleep(3)
但是呢 要在terminal输入:browser=Chrome pytest selenium_test/switch_to_frame.py -s -v
from selenium_base import Base
class Test_Js(Base):
def test_js(self):
self.driver.get('http://www.baidu.com')
self.driver.execute_script("document.querySelector('#kw').value='selenium测试'")
self.driver.execute_script("document.querySelector('#su').click()")
self.driver.execute_script("document.documentElement.scrollTop=10000")
self.sleep(2)
self.driver.execute_script("document.querySelectorAll('.page-inner_2jZi2 a')[8].click()")
self.sleep(5)
for code in [
'return document.title','return JSON.stringify(performance.timing)'
]:
print(self.driver.execute_script(code))
https://www.12306.cn/index/
def test_12306(self):
self.driver.get('https://www.12306.cn/index/')
self.driver.execute_script("a=document.querySelector('#train_date');a.value=''")
self.sleep(5)
https://image.baidu.com
from selenium_base import Base
class Test_Upload(Base):
def test_upload(self):
self.driver.get('https://image.baidu.com')
self.driver.find_element(by=self.By.ID,value='sttb').click()
self.driver.find_element(by=self.By.ID,value='stfile').send_keys('/Users/lianganqi/Desktop/python/img/baidu.jpg')
self.sleep(8)
from selenium_base import Base
from selenium.webdriver import ActionChains
class Test_Switch(Base):
def test_switch(self):
self.driver.get('https://www.runoob.com/try/try.php?filename=jqueryui-api-droppable')
self.driver.switch_to.frame('iframeResult')
drag = self.driver.find_element(by=self.By.ID,value="draggable")
drop = self.driver.find_element(by=self.By.ID,value="droppable")
action = ActionChains(self.driver)
action.drag_and_drop(drag,drop).perform()
self.sleep(2)
self.driver.switch_to.alert.accept()
self.driver.switch_to.default_content()
self.driver.find_element(by=self.By.ID,value='submitBTN').click()
self.sleep(3)
不会打开网页,但是一样能获取到数据。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。