我正在尝试访问http://www.academia.org.br/print/nossa-lingua/busca-no-vocabulario网站,该网站包含ABL(葡萄牙语:Academia Brasileira de Letras,英语:Brazil Academy of Letters)所考虑的单词,而Selenium令我头疼,因为总会出现需要修复的点。它检查是否在ABL网站上,是否按照我给他们的顺序答复:
“ Batata”:正确(正确)
“ Isqueiro”是对的(正确)
“ Batata”错误(发生了什么??)
“ Isqueiro”为真
“ Batata”错误
答案是多种多样的,但是我不知道原因,我做了一个计时器(它仍然在代码中),但是没有用!因此,我要问的是有人对它不起作用的原因有任何想法。我翻译了评论,但没有翻译变量,我希望它仍然可以理解。
代码1:
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import StaleElementReferenceException
def iniciar():
"""open the browser and accesses the ABL's page"""
global browser
options = Options()
prefs = {"profile.managed_default_content_settings.images": 2}
options.add_experimental_option("prefs", prefs)
options.add_argument("--headless") # Make the browser invisible
chrome_driver = "D:\programatio\PycharmProjects\pesquisador_de_palavras_no_vocabulario_da_abl\chromedriver_win32\chromedriver.exe"
browser = webdriver.Chrome(executable_path=chrome_driver, chrome_options=options)
browser.implicitly_wait(10)
browser.get("http://www.academia.org.br/print/nossa-lingua/busca-no-vocabulario")
def pesquisar(x: str) -> bool:
"""It is checked if the X variable is on the ABL's vocabulary"""
# Search and on it writes the variable X
findinput = browser.find_element_by_tag_name("input") # Search INTPUT
findinput.clear()
findinput.send_keys(x) # Writes the 'x' variable
# Search the button to press it
findbtn = browser.find_element_by_css_selector("button.btn.btn-primary") # Search the button
findbtn.click() # Click on it
# Search all the answers given by the site
WebDriverWait(browser, 10)
while True:
d = 0
try:
findpalavra = browser.find_elements_by_class_name("item-palavra")
except NoSuchElementException:
return False
except StaleElementReferenceException:
return False
# checks if on of the variables is equal to the 'x' variable and solves the "StaleElementReferenceException" error
if not findpalavra: # If not had been found
return False
try:
for d in range(0, len(findpalavra)):
if findpalavra[d].text == x:
return True
elif d == len(findpalavra) - 1:
return False
except StaleElementReferenceException:
pass
def sair():
"""The PESQUISADOR is closed"""
browser.quit()
代码2:
Pesquisador.iniciar()
a = ["batata", "isqueiro", "batata", "isqueiro", "batata"]
for c in range(0, len(a)):
print(Pesquisador.pesquisar(a[c]))
顺便说一句,对不起,如果我犯了一些拼写错误或无法理解。这不是我的母语(我敢打赌这不是您第一次听到!)。