(python笔记)python使用JS定位css元素

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
import datetime


class Zengtao_get(object):

    def __init__(self, url, account, password):
        self.driver = webdriver.Chrome()
        self.driver.get(url)
        self.driver.maximize_window()

        self.driver.find_element_by_name(name="account").send_keys(account)
        self.driver.find_element_by_name(name="password").send_keys(password)
        self.driver.find_element_by_id("submit").click()

        time.sleep(2)

    # def login(self, account, password):
    #     """
    #      # self.account = account
    #      # self.password = password
    #     """

    def search(self, value1, value2):
        # 搜索条件
        self.driver.find_element_by_xpath('//*[@id="bug-search"]/tbody/tr[2]/td/button[3]/i').click()
        self.driver.find_element_by_xpath('//*[@id="field1_chosen"]/a/span').click()
        self.driver.find_element_by_xpath('//*[@id="field1_chosen"]/div/ul/li[22]').click()  # 由谁创建
        self.driver.find_element_by_xpath('//*[@id="value1_chosen"]/a/span').click()
        self.driver.find_element_by_xpath('//*[@id="value1_chosen"]/a/div[2]/input').send_keys(value1)  # 输入搜索内容
        self.driver.find_element_by_xpath('//*[@id="value1_chosen"]/a/div[2]/input').send_keys(Keys.ENTER)

        self.driver.find_element_by_xpath('//*[@id="field2_chosen"]/a/span').click()
        self.driver.find_element_by_xpath('//*[@id="field2_chosen"]/div/ul/li[28]').click()  # 创建时间
        self.driver.find_element_by_xpath('//*[@id="value2"]').click()
        self.driver.find_element_by_xpath('//*[@id="value2"]').send_keys(value2)  # 输入当天日期
        self.driver.find_element_by_xpath('//*[@id="value2"]').send_keys(Keys.ENTER)

        self.driver.find_element_by_xpath('//*[@id="submit"]').click()  # 搜索
        time.sleep(2)
        # 获取当天创建bug数
        js = "return document.querySelector('.table-empty-tip')"  # return JS定位css输出值 document.querySelector
        bug = self.driver.execute_script(js)  # python调用js方法execute_script
        #判断元素是否存在
        if bug is None:
            bug_number = self.driver.find_element_by_xpath('//*[@id="bugForm"]/div[3]/div[3]/strong[1]').text
            print('本页共 %s 个bug' % bug_number)
        else:
            print('当天创建bug数 = 0')
        self.driver.quit()


if __name__ == '__main__':
    # value2 = time.strftime('%Y-%m-%d')   # 当天日期
    value2 = '2021-3-17'  
    Run = Zengtao_get('http://192.168.0.1/', 'lkj', '123456')
    # Run.login()
    Run.search('小学生', value2)

# driver.quit()

 纯手工,不喜勿喷

posted @ 2021-03-16 16:16  茫茫人海中的一颗沙尘  阅读(167)  评论(0编辑  收藏  举报