selenium自动刷题
欧洲开放大学mba 刷题
import time
from selenium import webdriver
from selenium.common import NoSuchElementException
from selenium.webdriver.common.by import By
from selenium.webdriver.edge.options import Options
from selenium.webdriver.edge.service import Service
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
def login(driver):
login_url = "https://academyeurope.eu/lp-profile/?redirect_to=https%3A%2F%2Facademyeurope.eu%2Flp-courses%2Fmba-diploma-program%2Fmaster-of-business-administration%2Flessons%2Fcorporate-social-responsibility-and-citizenship"
driver.get(login_url)
# 找到用户名输入框并输入用户名
username_input = driver.find_element(By.XPATH, '//*[@id="username"]')
username_input.send_keys("123123123")
# 找到密码输入框并输入密码
password_input = driver.find_element(By.XPATH, '//*[@id="password"]')
password_input.send_keys("123123.123123")
# 提交表单(假设有一个登录按钮)
submit_button = driver.find_element(By.XPATH, '//*[@id="learn-press-profile"]/div/div[1]/form/p[2]/button')
submit_button.click()
def restart(driver):
url = 'https://academyeurope.eu/lp-courses/mba-diploma-program/master-of-business-administration/'
driver.get(url)
btn = driver.find_element(By.CSS_SELECTOR, 'continue-course lp-button')
btn.click()
# 设置Edge浏览器的用户数据目录
edge_options = Options()
options = webdriver.EdgeOptions()
# options.add_experimental_option("detach", True)
# options.add_argument("--profile-directory=Profile\ 2")
options.add_argument('user-data-dir=/Users/arick/Library/Application Support/Microsoft Edge/Profile 2')
edge_options.add_argument("--remote-debugging-port=9222")
webdriver_path = "/Users/arick/go/bin/msedgedriver"
# 创建Chrome WebDriver并指定WebDriver文件位置
service = Service(webdriver_path)
# options.binary_location = "/Applications/Microsoft Edge.app/Contents/MacOS/Microsoft Edge --profile-directory=Profile 2"
# 创建一个 Chrome WebDriver
driver = webdriver.Edge(options=options,service=service)
# driver = webdriver.Chrome()
# driver = webdriver.Safari()
login(driver)
# 等待一段时间,让页面加载完成
driver.implicitly_wait(10)
# 打开网页
restart(driver)
driver.implicitly_wait(10)
# 等待直到按钮可见
for i in range(100):
wait = WebDriverWait(driver, 10)
btn = '//*[@id="learn-press-content-item"]/div/div/div[2]/form/button'
try:
button = driver.find_element(By.CLASS_NAME, 'lp-btn-complete-item')
# 点击按钮
button.click()
except NoSuchElementException as e:
print(e)
restart(driver)
continue
wait = WebDriverWait(driver, 10)
btn2 = '/html/body/div[4]/div/div/div[3]/button[2]'
button2 = driver.find_element(By.CLASS_NAME, 'btn-yes')
# 点击按钮
button2.click()
time.sleep(3)
# 关闭浏览器
# driver.quit()
import time
from selenium import webdriver
from selenium.common import NoSuchElementException
from selenium.webdriver.common.by import By
from selenium.webdriver.edge.options import Options
from selenium.webdriver.edge.service import Service
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
def login(driver):
login_url = "https://academyeurope.eu/lp-profile/?redirect_to=https%3A%2F%2Facademyeurope.eu%2Flp-courses%2Fmba-diploma-program%2Fmaster-of-business-administration%2Flessons%2Fcorporate-social-responsibility-and-citizenship"
driver.get(login_url)
# 找到用户名输入框并输入用户名
username_input = driver.find_element(By.XPATH, '//*[@id="username"]')
username_input.send_keys("123123")
# 找到密码输入框并输入密码
password_input = driver.find_element(By.XPATH, '//*[@id="password"]')
password_input.send_keys("123.123123")
# 提交表单(假设有一个登录按钮)
submit_button = driver.find_element(By.XPATH, '//*[@id="learn-press-profile"]/div/div[1]/form/p[2]/button')
submit_button.click()
def restart(driver):
url = 'https://academyeurope.eu/lp-courses/mba-diploma-program/transcription-of-mba-program/'
driver.get(url)
driver.implicitly_wait(20)
try:
btn = driver.find_element(By.CSS_SELECTOR, 'div.lp-course-buttons > form > button')
btn.click()
except NoSuchElementException as e:
print(e)
driver.implicitly_wait(10)
# 设置Edge浏览器的用户数据目录
edge_options = Options()
options = webdriver.EdgeOptions()
# options.add_experimental_option("detach", True)
# options.add_argument("--profile-directory=Profile\ 2")
options.add_argument('user-data-dir=/Users/arick/Library/Application Support/Microsoft Edge/Profile 2')
edge_options.add_argument("--remote-debugging-port=9222")
webdriver_path = "/Users/arick/go/bin/msedgedriver"
# 创建Chrome WebDriver并指定WebDriver文件位置
service = Service(webdriver_path)
# options.binary_location = "/Applications/Microsoft Edge.app/Contents/MacOS/Microsoft Edge --profile-directory=Profile 2"
# 创建一个 Chrome WebDriver
driver = webdriver.Edge(options=options,service=service)
# driver = webdriver.Chrome()
# driver = webdriver.Safari()
login(driver)
# 等待一段时间,让页面加载完成
driver.implicitly_wait(10)
# 打开网页
restart(driver)
driver.implicitly_wait(10)
# 等待直到按钮可见
for i in range(13):
wait = WebDriverWait(driver, 10)
btn = '//*[@id="learn-press-content-item"]/div/div/div[2]/form/button'
try:
choice1 = driver.find_element(By.CSS_SELECTOR, '.choice-1 .wpforms-field-label-inline')
choice1.click()
choice2 = driver.find_element(By.CSS_SELECTOR, '.choice-2 .wpforms-field-label-inline')
choice2.click()
choice3 = driver.find_element(By.CSS_SELECTOR, '.choice-3 .wpforms-field-label-inline')
choice3.click()
elements = driver.find_elements(By.CLASS_NAME, 'wpforms-field-text')
for e in elements:
print(e.text)
e.find_element(By.TAG_NAME,'input').send_keys("Dear Sir/Madam")
first = driver.find_element(By.CLASS_NAME, 'wpforms-field-name-first')
first.send_keys("123123")
last = driver.find_element(By.CLASS_NAME, 'wpforms-field-name-last')
last.send_keys("123123")
email = driver.find_element(By.CSS_SELECTOR, 'div.wpforms-field-email > input')
email.send_keys("123123@qq.com")
sub = driver.find_element(By.CLASS_NAME, 'wpforms-submit')
sub.click()
# 等待一段时间,让页面加载完成
driver.implicitly_wait(50)
time.sleep(5)
sub2 = driver.find_element(By.CLASS_NAME, 'button-complete-lesson')
sub2.click()
button2 = driver.find_element(By.CLASS_NAME, 'btn-yes')
# 点击按钮
button2.click()
time.sleep(5)
# break
except NoSuchElementException as e:
print(e)
restart(driver)
continue
# 关闭浏览器
# driver.quit()