1.用新学的Select类方法实现,选择抖音,但是要判断,如果抖音没被选择,才会去选择
from selenium import
webdriver
from time import
sleep
from
selenium.webdriver.support.select import Select
driver =
webdriver.Chrome()
url = ‘file://C:/select.htm‘
driver.get(url)
driver.maximize_window()
sleep(2)
下拉框_loc = ‘//select’
下拉框 = driver.find_element_by_xpath(下拉框_loc)
print(Select(下拉框).first_selected_option.text)
if
Select(下拉框).first_selected_option.text == ‘抖音’:
print(‘抖音本身已被选择’)
else:
Select(下拉框).select_by_visible_text(‘抖音’)
sleep(2)
driver.quit()
2.用鼠标键盘模拟事件,去完成今日头条的选择
from selenium import
webdriver
from time import
sleep
from
selenium.webdriver import ActionChains
from
selenium.webdriver.common.keys import Keys
driver =
webdriver.Chrome()
url = ‘file://C:/select.htm‘
driver.get(url)
driver.maximize_window()
sleep(2)
xia_la_kuang_loc =
‘//select’
xia_la_kuang =
driver.find_element_by_xpath(xia_la_kuang_loc)
xia_la_kuang.click()
#
ActionChains(driver).key_down(Keys.DOWN).key_up(Keys.DOWN).perform()
ActionChains(driver).send_keys(Keys.DOWN).send_keys(Keys.ENTER).perform()
sleep(2)
driver.quit()
3.百度高级搜索里,【时间:限定要搜索的网页的时间是】选择最近一年
fromseleniumimportwebdriver
fromtimeimportsleep
fromselenium.webdriverimportActionChains
fromselenium.webdriver.common.keysimportKeys
driver=webdriver.Chrome()
url=‘http://www.baidu.com‘
driver.get(url)
driver.maximizewindow()
shezhi_loc=‘//span[@id=”s-usersetting-top”]’
shezhi=driver.find_element_by_xpath(shezhi_loc)
#ActionChains(driver).contextclick(baidu).perform()
ActionChains(driver).move_to_element(shezhi).perform()
sousuoshezhi_loc=‘//a[@class=”setpref”and@href=”javascript:;”]’
sousuoshezhi=driver.find_element_by_xpath(sousuoshezhi_loc).click()
sleep(1)
gaolisousuo_loc=‘//li[@data-tabid=”advanced”]’
gaolisousuo=driver.find_element_by_xpath(gaolisousuo_loc).click()
sousuojieguo_loc=‘//input[@id=”adv_keyword”]’
guanjianci=‘凡猫’
sousuojieguo=driver.find_element_by_xpath(sousuojieguo_loc).send_keys(guanjianci)
quanbushijian_loc=‘//span[@id=”adv-setting-gpc”]//i[@class=”c-iconc-select-arrow”]’
quanbushijian=driver.find_element_by_xpath(quanbushijian_loc).click()
yinian_loc=‘//p[text()=”最近一年”]’
yinian=driver.find_element_by_xpath(yinian_loc)
yinian.click()
sleep(2)
gaoji_loc=‘//input[@value=”高级搜索”]’
gaoji=driver.find_element_by_xpath(gaoji_loc).click()
sleep(1)
sleep(3)
driver.quit()
4智能等待
from time import
sleep
from selenium import
webdriver
from
selenium.webdriver.support.wait import WebDriverWait
from
selenium.webdriver.support import expected_conditions as EC
from
selenium.webdriver.common.by import By
driver =
webdriver.Chrome()
driver.implicitly_wait(10)
driver.maximize_window()
url = ‘https://www.juhe.cn/‘
driver.get(url)
login_window_frame_loc
= (By.XPATH, ‘//iframe[@id=”layui-layer-iframe1”]’)
# login_window_frame
= driver.find_element_by_xpath(login_window_frame_loc)
WebDriverWait(driver,
1).until(EC.frame_to_be_available_and_switch_to_it(login_window_frame_loc))
sleep(1)
# telephone_loc =
(By.XPATH, ‘//input[@id=”mobilephone”]’)
# # telephone =
driver.find_element_by_xpath(telephone_loc)
# aaa =
WebDriverWait(driver,
20).until(EC.visibility_of_element_located(telephone_loc))
# print(aaa)
#
#
# deng_lu_loc =
(By.XPATH, ‘//input[@id=”secondBtn”]’)
# bbb =
WebDriverWait(driver, 20).until(EC.visibility_of_element_located(deng_lu_loc))
# print(bbb)
#
login_window_frame_loc = ‘//iframe[@id=”layui-layer-iframe1”]’
# login_window_frame
= driver.find_element_by_xpath(login_window_frame_loc)
#
driver.switch_to.frame(login_window_frame)
#
WebDriverWait(driver,
20).until(EC.invisibility_of_element_located(login_window_frame))
#
telephone_loc =
‘//input[@id=”mobilephone”]’
telephone =
driver.find_element_by_xpath(telephone_loc)
telephone.send_keys(13812345678)
sleep(2)
driver.quit()
智能解释
from selenium import webdriver
#driver=webdriver.Chrome()
##__隐式等待
#driver.implicitly_wait(10)
#driver.maximize_window()
#driver.get(‘https://www.juhe.cn/‘)
##智能等待,显式等待
#__from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expectedconditions as EC
#from selenium.webdriver.support.expectedconditionsimportvisibility_of_element_located
_#from selenium.webdriver.support.expectedconditionsimportframe_to_be_available_and_switch_to_it
##WebDriverWait()有两个参数:1、你目前所使用的driver;2、智能等待的设置时间
##.until(条件句)配合WebDriverWait的使用
##定义元组形式的定位器,如果使用xpath表达元素,就写By.XPATH
#__from
selenium.webdriver.common.by import By
#弹出的窗口_frame_loc=(By.XPATH,’//iframe[@id=”layui-layer-iframe1”]’)
##frame_to_be_available_and_switch_to_it()这个条件方法,参数填写你要等待和切入的frame元素
#wait=WebDriverWait(driver,20)
Wait.until(frame_to_be_available_and_switch_to_it(弹出的窗口_frame_loc))
##visibility_of_element_located()这个条件方法,参数填写你要智能等待的元素的“元组类型”的定位器表达式,
##如果在设置的时间内,等待到元素,则返回元素,反之,Python会抛出“等待超时”这个异常
#__wait=WebDriverWait(driver,20)
#手机号码_loc=(By.XPATH,’//input[@id=”mobilephone”]’)
##直接在visibility_of_element_located()方法后面放入“元组形态”的定位器表达式
##等待成功,则返回元素
#手机号码=wait.until(visibility_of_element_located(手机号码_loc))
#print(‘元素长这样:{}’.format(手机号码))
#手机号码.send_keys(‘13812345678’)
#from time import sleep
#sleep(2)
#
#driver.quit()