python selenium js operation, mouse operation, button operation, WebDriverWait, unitl expected_conditions, select drop-down box

from selenium import webdriver

#在期望条件下去执行
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
#显性等待引入
from selenium.webdriver.support.wait import WebDriverWait

import time

url='https://ke.qq.com/'
driver = webdriver.Firefox()
"""
#全局通用化隐性等待
driver.implicitly_wait(20)

#强制等待
time.sleep(10)

"""
#浏览器窗口最大化
driver.maximize_window()

driver.get(url)

Wait = WebDriverWait(driver,10,2)
#【located的参数要以元组形式传入】
xpath = '//a[@id="js_login"]'
located=(By.XPATH,xpath)  #【元组】
Wait.until(EC.visibility_of_element_located(located))
driver.find_element_by_xpath('//a[@id="js_login"]').click()
Wait.until(EC.visibility_of_element_located((By.XPATH,"//i[@class='icon-font i-qq']")))
driver.find_element_by_xpath("//i[@class='icon-font i-qq']").click()
#等待iframe出现后切换至frame中
Wait.until(EC.frame_to_be_available_and_switch_to_it((By.NAME,'login_frame_qq')))
Wait.until(EC.visibility_of_element_located((By.ID,'switcher_plogin')))
driver.find_element_by_id('switcher_plogin').click()

driver.quit()

-------------------------

#下拉框处理,鼠标操作
from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.select import Select  # 引入下拉框操作的类模块
from selenium.webdriver.common.keys import Keys # 部分实体按键操作模拟库
import time

driver = webdriver.Firefox()
url = "https:www.baidu.com"

driver.get(url)
element = driver.find_element_by_xpath('//div[@id="u1"]//a[@name="tj_settingicon"]')
#实例化ActionChains类 ,鼠标调用最后一定要用perform()才会有执行效果。
ActionChains(driver).move_to_element(element).perform()
time.sleep(10)
ActionChains(driver).move_to_element(element).click()
WebDriverWait(driver,10).until(EC.visibility_of_element_located((By.XPATH,'//div[@class="bdpfmenu"]//a[@target="_blank"]')))
#获取点击打开新窗口之前的窗口数以判断是否有新窗口打开
# handles = driver.window_handles
driver.find_element_by_xpath('//a[text()="高级搜索"]').click()
# driver.find_element_by_xpath('//div[@class="bdpfmenu"]//a[@target="_blank"]').click()
#等待新窗口打开
# WebDriverWait(driver,10).until(EC.new_window_is_opened(handles))
WebDriverWait(driver,10).until(EC.visibility_of_element_located((By.XPATH,'//select[@name="ft"]')))

#1通过元素的方式进行定位点击选择下拉选项
driver.find_element_by_xpath('//select//option[@value="pdf"]').click()
time.sleep(2)
driver.find_element_by_xpath('//select//option[@value="doc"]').click()
time.sleep(2)
driver.find_element_by_xpath('//select//option[@value="xls"]').click()

#2 直接使用select执行选择下拉选项
# 初始化Select()传入下拉定位的元素
select = Select(driver.find_element_by_xpath('//select[@name="ft"]'))
#通过select中的方法直接取值或者定位
select.select_by_value('doc')
time.sleep(2)
select.select_by_value('pdf')
time.sleep(2)
select.select_by_value('xls')

#根据下拉选项的文本进行选择
select.select_by_visible_text('Adobe Acrobat PDF (.pdf)')

driver.quit()

-----------------------------------
from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.by import By
#selenium中封装的按键操作,一些按键操作可以通过keys进行组合
from selenium.webdriver.common.keys import Keys
import time
driver = webdriver.Firefox()
url='https://www.baidu.com'
driver.maximize_window()
driver.get(url)
driver.find_element_by_xpath('//input[@id="kw"]').send_keys('python',Keys.ENTER)
WebDriverWait(driver,10).until(EC.visibility_of_element_located((By.XPATH,'//div[@id="page"]//span[text()=2]')))
#输入文字后按下回车
ele=driver.find_element_by_xpath('//div[@id="page"]//span[text()=2]')
#执行脚本滑动页面到可见该元素位置
driver.execute_script("arguments[0].scrollIntoView();",ele)
time.sleep(3)
#移动元素底端与页面底端对齐
driver.execute_script("arguments[0].scrollIntoView(false)",ele)
time.sleep(3)
# 移动到页面底部
driver.execute_script("window.scrollTo(0,document.body.scrollHeight)")
time.sleep(3)
#移动到页面顶部
driver.execute_script("window.scrollTo(document.body.scrollHeight,0)")

driver.quit()

-----------------------------------

from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
import time
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains #鼠标操作

driver= webdriver.Chrome()
url="https://www.12306.cn"
driver.get(url)

WebDriverWait(driver,10).until(EC.visibility_of_element_located((By.ID,"fromStationText")))
time.sleep(5)
#鼠标移动后点击输入框输入成都
ActionChains(driver).move_to_element(driver.find_element_by_id('fromStationText')).click().perform()
driver.find_element_by_id('fromStationText').send_keys("成都")
WebDriverWait(driver,10).until(EC.visibility_of_element_located((By.XPATH,'//div[@id="citem_2"]//span[text()="成都"]')))
driver.find_element_by_xpath('//div[@id="citem_2"]//span[text()="成都"]').click()
time.sleep(3)
# driver.find_element_by_id("fromStationText").send_keys("上海",Keys.ENTER)
driver.find_element_by_id("toStationText").send_keys('乌鲁木齐南',Keys.ENTER)
#执行js脚本修改无法输入的日期显示
driver.execute_script("els=document.getElementById('train_date');els.removeAttribute;els.value='2019-03-26';")
WebDriverWait(driver,10).until(EC.visibility_of_element_located((By.ID,"search_one")))
driver.find_element_by_id("search_one").click()

time.sleep(10)
driver.quit()

 

Guess you like

Origin blog.csdn.net/dance117/article/details/88773998