首页 > 解决方案 > Python seleium 不会从屏幕边缘刮掉元素

问题描述

我正在创建一个 python seleium 机器人,它可以完美地从教育中删除答案(我学校使用的一个学习平台),然后使用 pyautogui 回答问题,该程序可以运行,直到它被问到一个问题,其中答案已经离开了屏幕的底部。[网站照片][1] [1]:https://i.stack.imgur.com/KT9YB.jpg HTML 显示了答案,但 seleium 没有刮掉它们,它只是留下空白 这里是问题列表和答案['元, yuán', '什么, shénme', '少, shǎo', '百, bǎi', '两, liǎng', '要, yào', '钱, qián', '买, mǎi', '件, jiàn', '裙子, qúnzi', '裤子, kùzǐ', '卖, mài', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''] ['Yuan; dollar; Chinese dollar', 'what', 'little; few', 'hundred', 'two; a couple; two of something', 'to want', 'cash; money', 'to buy', 'measure word for clothes', 'skirt', 'trousers; pants', 'to sell', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '']

这是我的代码:

from typing import Dict
from selenium.webdriver.common.keys import Keys
from selenium import webdriver
import pyautogui
import time
#Education Perfect Bot V2
loop = 1
data_answers = []
data_questions = []
#Defines the the data collection function
def ep_data(q,a,i):
    data_question = driver.find_element_by_xpath(f'/html/body/main/div[3]/div/student-app-wrapper/div[1]/div[2]/div/ui-view/div/div[2]/div/div[2]/div[4]/div[1]/div[1]/div[{i}]/div[1]/div[1]').text
    data_answer = driver.find_element_by_xpath(f'/html/body/main/div[3]/div/student-app-wrapper/div[1]/div[2]/div/ui-view/div/div[2]/div/div[2]/div[4]/div[1]/div[1]/div[{i}]/div[1]/div[2]').text
    q.append(data_question)
    a.append(data_answer)


#Inputs to log into EP
print ('Education Perfect Bot (V2)')
link = input('Link to task: ')

username = 'user'
password = 'pass'

#Usr/PassWd
#username = input('Enter Username: ')
#password = input('Enter Password: ')

question_amount = 3
#question_amount = int(input('Question Amount (1)5, (2)10, (3)20, (4)50, (5)Infinity: '))

#Opens EP
driver = webdriver.Firefox(executable_path=r'C:\Program Files (x86)\geckodriver.exe')
driver.get(link)
time.sleep(3)
pyautogui.write(username)
pyautogui.press('tab')
pyautogui.write(password)
pyautogui.press('enter')
time.sleep(10)
driver.find_element_by_xpath(f'//*[@id="number-of-questions-selector"]/li[{question_amount}]').click()

#Collects data
while True:
    try:
        ep_data(data_questions, data_answers, loop)
        loop += 1
    except:
        break

data_questions = [q.replace(';', ',') for q in data_questions]
print(data_questions, '\n', data_answers, '\n')

#Genereate question and answer dict 
QAzip = zip(data_questions, data_answers)
QADict = dict(QAzip)
print(QADict, '\n')  #Prints question and answer dict

#Enters Task
driver.find_element_by_tag_name('body').send_keys(Keys.ENTER)
time.sleep(5)

#Finds answer
#task_question = driver.find_element_by_xpath('/html/body/main/div[3]/div/student-app-wrapper/div[1]/div[2]/div[1]/ui-view/div[1]/div[2]/div/div/div[1]/div[2]/div/div[2]/div[2]/span[2]').text
#print(task_question)
#task_answer = QADict[task_question]
#print(task_answer)

#Inputs Answers
while True:
    try:
        task_question = driver.find_element_by_xpath('/html/body/main/div[3]/div/student-app-wrapper/div[1]/div[2]/div[1]/ui-view/div[1]/div[2]/div/div/div[1]/div[2]/div/div[2]/div[2]/span[2]').text
        print(task_question)
        task_answer = QADict[task_question]
        print(task_answer)
        pyautogui.write(task_answer)
        pyautogui.press('enter')
    except:
        print('Task Complete')
        break

标签: pythonhtmlpython-3.xselenium

解决方案


推荐阅读