某oj selenium 无需界面 提交代码 自动更新cookie免登陆

在这里插入图片描述

alias AC='sudo time python3 up_code.py'
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.common.by import By
import time
import re
import requests
import sys
import os
import json

class UpCodeSpider:
    def __init__(self, problem, file):
        option = webdriver.ChromeOptions()
        option.add_argument('headless')
        option.add_argument('no-sandbox')
        option.add_argument('disable-dev-shm-usage')
        self.browser = webdriver.Chrome('bin/chromedriver',chrome_options=option)

        self.username = ''
        self.password = ''
        self.cookie_file = 'ookies'
        self.title = 'null'
        self.problem = problem
        self.file = file
        self.browser.get('http://ox.com/')
        with open(self.file, 'r') as file:
            self.code = file.read()
        with open(self.cookie_file, "r") as f:
            cookies = json.load(f)
            for cookie in cookies:
                self.browser.add_cookie(cookie)
    def up_login(self):
        print('正在登录' + self.username)
        self.browser.get('https://wwcom/login')
        try:
            self.browser.find_element_by_xpath('/html/body/div[1]/header/nav/div/ul/li[2]/a').click()
            self.browser.find_element_by_xpath('//*[@id="login_username"]').send_keys(self.username)
            self.browser.find_element_by_xpath('//*[@id="login_password"]').send_keys(self.password)
            self.browser.find_element_by_xpath('//*[@id="login-form"]/div[4]/button').click()
        except:
            pass
        self.browser.get('http://.com/')
        new_cookies = self.browser.get_cookies()
        jsonCookies = json.dumps(new_cookies)
        with open(self.cookie_file, 'w') as f:
            f.write(jsonCookies)
        with open(self.cookie_file, "r") as f:
            cookies = json.load(f)
            for cookie in cookies:
                self.browser.add_cookie(cookie)

    def start(self):
        self.browser.get('http:///problem/' + str(self.problem))
        try:
            test = self.browser.find_element_by_link_text('登出')
        except Exception as e:
            print('cookies失效,重新登录中')
            self.up_login()
        self.browser.get('http://m/problem/' + str(self.problem))
        WebDriverWait(self.browser, 5, 1).until(lambda x: x.find_element_by_link_text('提交'))
        self.browser.find_element_by_link_text('提交').click()
        print('进入提交')
        WebDriverWait(self.browser, 5, 1).until(lambda x: x.find_element_by_xpath('//*[@id="div-answer_answer_editor"]/div[1]'))
        self.browser.find_element_by_xpath('//*[@id="div-answer_answer_editor"]/div[1]').click()
        self.browser.find_element_by_xpath('//*[@id="div-answer_answer_editor"]/div[1]/div[6]').click()
        self.browser.find_element_by_xpath('//*[@id="div-answer_answer_editor"]/div[1]/div[6]/div[1]').click()
        self.browser.find_element_by_xpath('//*[@id="div-answer_answer_editor"]/div[1]/div[6]/div[1]/div').click()
        self.browser.find_element_by_xpath('//*[@id="div-answer_answer_editor"]/div[1]/div[6]/div[1]/div/div').click()
        self.browser.find_element_by_xpath('//*[@id="div-answer_answer_editor"]/div[1]/div[6]/div[1]/div/div/div').click()
        self.browser.find_element_by_xpath('//*[@id="div-answer_answer_editor"]/div[1]/div[6]/div[1]/div/div/div/div[5]').click()
        self.browser.find_element_by_xpath('//*[@id="div-answer_answer_editor"]/div[1]/div[6]/div[1]/div/div/div/div[5]/div').click()
        self.browser.find_element_by_xpath('//*[@id="div-answer_answer_editor"]/div[1]/div[6]/div[1]/div/div/div/div[5]/div/pre').click()
        self.browser.find_element_by_xpath('//*[@id="div-answer_answer_editor"]/div[1]/div[1]/textarea').send_keys(self.code)
        self.browser.find_element_by_xpath('//*[@id="button-submit-answer"]').click()
        print('粘贴代码')
        print(self.browser.find_element_by_xpath('/html/body/div/div[3]/div[2]/table/tbody/tr[1]/td[4]/a').text)
        try:
            while self.browser.find_element_by_xpath('/html/body/div/div[3]/div[2]/table/tbody/tr[1]/td[4]/a').text == 'Waiting':
                pass
        except Exception as e:
            #print(e)
            pass
        print(self.browser.find_element_by_xpath('/html/body/div/div[3]/div[2]/table/tbody/tr[1]/td[4]/a').text)
spider = UpCodeSpider(sys.argv[1], sys.argv[2])
spider.start()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值