美文网首页
Python+Selenium自动化模拟登录站酷

Python+Selenium自动化模拟登录站酷

作者: 清风昙 | 来源:发表于2022-04-23 16:00 被阅读0次
    """
    @Time: 2022/04/17
    @Auth: HouPinTan
    """
    
    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from time import sleep
    from selenium.webdriver.support.wait import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    
    
    class Zcool:
        def __init__(self, path, url):
            self.chromePath = path
            self.url = url
    
        def open_url(self):
            """
            打开网页
            :return:
            """
            self.webDriver = webdriver.Chrome()
            # self.webDriver = webdriver.Edge()
            # self.webDriver = webdriver.Ie()
            self.webDriver.get(self.url)
            self.webDriver.maximize_window()
    
        def login(self, username, password):
            """
            登录站酷
            "return:
            """
    
            btnLogin = self.webDriver.find_elements(
                By.CLASS_NAME, "header_login")
            print('btnLogin')
            for btn in btnLogin:
                print(btn.text)
                print('btn')
                if (btn.text == "登录"):
                    btn.click()
                    break
            sleep(1)
            # 跳转到iframe页面
            self.webDriver.switch_to.frame('loginChild')
            sleep(2)
            # 密码登录
            self.webDriver.find_element(
                "xpath", "//*[@id='login-box']/div[2]/div[1]/div[2]").click()
            # sleep(1)
            WebDriverWait(self.webDriver, 5).until(EC.presence_of_element_located(
                (By.ID, "username")))
            self.webDriver.find_element(
                'id', 'username').send_keys(username)
            self.webDriver.find_element(
                'id', 'password').send_keys(password)
            # sleep(1)
            WebDriverWait(self.webDriver, 5).until(
                EC.presence_of_element_located((By.ID, "loginbtn")))
            self.webDriver.find_element('id', 'loginbtn').click()
            sleep(5)
            self.webDriver.find_element('xpath', '//*[@id="close"]').click()
    
        def search(self):
            """
            搜索
            :return:
            """
            self.webDriver.find_element_by_class_name('gukrch').click()
            # sleep(1)
            WebDriverWait(self.webDriver, 5).until(
                EC.presence_of_element_located((By.ID, "headerSearchInput")))
            self.webDriver.find_element(
                'id', 'headerSearchInput').send_keys('满月茶礼')
            # sleep(1)
            WebDriverWait(self.webDriver, 5).until(
                EC.presence_of_element_located((By.CLASS_NAME, "search-icon")))
            self.webDriver.find_element_by_class_name('search-icon').click()
    
        def detail(self):
            """
            详情
            :return:
            """
            self.webDriver.find_element_by_class_name('fjGFwo').click()
            # 返回所有的window窗口,是一个List
            windows = self.webDriver.window_handles
            # 真正切换到新的窗口
            self.webDriver.switch_to.window(windows[1])
            sleep(2)
            self.webDriver.find_element(
                'xpath', '//*[@id="__next"]/div/main/div/div[3]/div/div[3]/div/figure/a/img').click()
    
    
    if __name__ == '__main__':
        zcool = Zcool('', 'https://www.zcool.com.cn/')
        zcool.open_url()
        zcool.login('13622527616', 'hpt123456')
        zcool.search()
        zcool.detail()
    
    

    相关文章

      网友评论

          本文标题:Python+Selenium自动化模拟登录站酷

          本文链接:https://www.haomeiwen.com/subject/gqrzertx.html