forked from mrlan/EnglishPal
参考了一下tangxinyuan小组的写法,将选取单词的方法由模拟鼠标选取单词改为根据id获取文章,随机从文章中挑选一个单词放入生词框中,这样是一定可以选取到文章中的单词。
然后根据span标签定位到选择的生词,双击再次选择,从而在生词框中删除该单词Bug545-HuangHuiLing
parent
4a42c5c22c
commit
a0c9b82ee7
|
@ -8,33 +8,24 @@ from selenium.webdriver.common.action_chains import ActionChains
|
||||||
|
|
||||||
from helper import signup
|
from helper import signup
|
||||||
|
|
||||||
|
def has_punctuation(s):
|
||||||
|
return any(c in string.punctuation for c in s)
|
||||||
|
|
||||||
def select_one(driver):
|
def select_one(driver):
|
||||||
question = driver.find_element(By.ID, 'question')
|
elem = driver.find_element(By.ID, 'article')
|
||||||
# 创建ActionChains对象
|
essay_content = elem.text
|
||||||
actions = ActionChains(driver)
|
valid_word = random.choice([word for word in essay_content.split() if len(word) >= 6 and not has_punctuation(
|
||||||
|
word) and 'font>' not in word and 'br>' not in word and 'p>' not in word])
|
||||||
# 模拟鼠标移动到文本元素上
|
driver.find_element(By.ID, 'selected-words').send_keys(valid_word)
|
||||||
actions.move_to_element(question)
|
driver.find_element(By.ID, 'article').click()
|
||||||
|
return valid_word
|
||||||
# 模拟鼠标按下并拖动以选择文本
|
|
||||||
actions.double_click()
|
|
||||||
actions.perform()
|
|
||||||
|
|
||||||
# 获取选中的文本
|
|
||||||
return driver.find_element(By.ID, 'selected-words').get_attribute('value')
|
|
||||||
|
|
||||||
def select_two(driver):
|
def select_two(driver):
|
||||||
question = driver.find_element(By.ID, 'question')
|
word = driver.find_element(By.CLASS_NAME, 'highlighted')
|
||||||
article = driver.find_element(By.ID, 'article_title')
|
|
||||||
|
|
||||||
# 创建ActionChains对象
|
# 创建ActionChains对象
|
||||||
actions = ActionChains(driver)
|
actions = ActionChains(driver)
|
||||||
actions.move_to_element(article)
|
actions.move_to_element(word)
|
||||||
actions.click()
|
|
||||||
time.sleep(1)
|
|
||||||
# 模拟鼠标移动到文本元素上
|
|
||||||
actions.move_to_element(question)
|
|
||||||
|
|
||||||
# 模拟鼠标按下并拖动以选择文本
|
# 模拟鼠标按下并拖动以选择文本
|
||||||
actions.double_click()
|
actions.double_click()
|
||||||
|
@ -45,12 +36,6 @@ def test_selected_second_word(driver, URL):
|
||||||
try:
|
try:
|
||||||
signup(URL, driver)
|
signup(URL, driver)
|
||||||
selected_words = select_one(driver);
|
selected_words = select_one(driver);
|
||||||
while selected_words.strip() == "":
|
|
||||||
load_next_article = driver.find_element(By.ID, "load_next_article")
|
|
||||||
action_chains = ActionChains(driver)
|
|
||||||
action_chains.click(load_next_article).perform()
|
|
||||||
time.sleep(1)
|
|
||||||
selected_words = select_one(driver)
|
|
||||||
assert selected_words.strip() != "", "选中的单词被放置框中"
|
assert selected_words.strip() != "", "选中的单词被放置框中"
|
||||||
select_two(driver)
|
select_two(driver)
|
||||||
selected_second_words = driver.find_element(By.ID, 'selected-words').get_attribute('value')
|
selected_second_words = driver.find_element(By.ID, 'selected-words').get_attribute('value')
|
||||||
|
|
Loading…
Reference in New Issue