python+selenium常見實例-根據日曆進行查詢(上)

一、思路:
1、查找日曆元素;
2;設置日曆時間;
3、查找搜索元素;
4、點擊搜索按鈕;
5、查看列表元素
6、打印列表中的信息
7、判斷日期是否正確

二、重難點
1、設置日曆時間
2、打印所查詢的列表中的信息

三、解決辦法
日曆設置:請參考該鏈接:https://blog.csdn.net/duzilonglove/article/details/78246903;


二、實例

def rili(driver):
##############################################日曆控件元素ID+日曆設置#######################
    js1 = 'document.getElementById("openWorkDateStart").removeAttribute("readonly");'
    driver.execute_script(js1)
    js1_value = 'document.getElementById("openWorkDateStart").value="2018-10-5"'
    driver.execute_script(js1_value)
    js2 = 'document.getElementById("openWorkDateEnd").removeAttribute("readonly");'
    driver.execute_script(js2)
    js2_value = 'document.getElementById("openWorkDateEnd").value="2018-10-22"'
    driver.execute_script(js2_value)
#############################搜索按鈕元素+搜索按鈕點擊#####################################################
    driver.find_element_by_xpath("/html/body/div[3]/table/tbody/tr/td[5]/span/a[1]/span").click()
    time.sleep(3)
###############################此處是當起始日期大於結束日期彈框處理###########
  try:
    string = driver.switch_to_alert().text
    print(string)
    driver.switch_to_alert().accept()
  except:
    pass
#################################################

打印列表信息:
重難點:1、如何定位元素:將鼠標放在第一行數據查看元素。然後放在第二行看元素,會發現,其實是有規律可行
2、使用find_elements_by_xpath,記住是elements,因爲包含很多行的相同元素,且元素屬於list型

def xierujieguo(driver):
##############找到列表標題進行打印
    text0=driver.find_element_by_xpath("/html/body/form/div/table[1]")
print(text0.text)
###########################找到列表信息中的所有元素,進行打印#################################
    text1 = driver.find_elements_by_xpath("/ html / body / form / div / table[1] / tbody ")
    for t in text1:
        print(t.text)
    


 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章