如何解决此超时异常,我想在命令行上打印文本

时间:2018-12-24 11:28:01

标签: python selenium

我要使用python和selenium来使用Google翻译,我要解析args到Google翻译并在我的cmd上返回翻译后的语句并打印出来 但我遇到了例外情况

Traceback (most recent call last):
  File "Translator.py", line 27, in <module>
    TranslateButton = WebDriverWait(driver, 100).until(lambda driver: driver.find_element_by_xpath(TranslateElement))
  File "C:\Users\Prasad\AppData\Local\Programs\Python\Python36\lib\site-packages\selenium\webdriver\support\wait.py", line 80, in until
    raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:  

我尝试使用chromedriver,也尝试增加时间限制

from selenium import webdriver

from selenium.webdriver.support.ui import WebDriverWait

from time import sleep

import argparse


Languages = ('ta', 'mn', 'tl', 'cy', 'kn', 'sn', 'ko', 'ar', 'km', 'bs', 'la', 'cs', 'rw', 'ne', 'sq', 'sw', 'sm', 'mi', 'co', 'hy', 'el', 'st', 'tk', 'ha', 'it', 'sd', 'af', 'sr', 'es', 'bn', 'th', 'is', 'ka', 'ht', 'so', 'ga', 'fr', 'hi', 'ig', 'pt', 'tg', 'ro', 'pl', 'vi', 'en', 'lv', 'mk', 'hr', 'mt', 'te', 'lt', 'ru', 'id', 'iw', 'gl', 'sv', 'ms', 'sl', 'zh-TW', 'ug', 'zh', 'mr', 'fi', 'si', 'xh', 'jw', 'fy', 'pa', 'ku', 'ps', 'hu', 'ml', 'no', 'gd', 'eu', 'or', 'ceb', 'fa', 'bg', 'lb', 'kk', 'et', 'gu', 'lo', 'yi', 'su', 'da', 'am', 'ur', 'hmn', 'de', 'tr', 'tt', 'eo', 'yo', 'sk', 'zu', 'nl', 'be', 'ky', 'haw', 'uk', 'ca', 'zh-CN', 'my', 'uz', 'mg', 'ny', 'ja', 'az')


parser = argparse.ArgumentParser(description = "Translate everything!")

parser.add_argument('--source-language', required = True, choices = Languages, help="Specify the source language!")

parser.add_argument('--destination-language', required = True, choices = Languages, help="Specify the destination language!")

parser.add_argument('-tobetranslated', required = True, help="What do you need translating?")

args = parser.parse_args()

if args.source_language == args.destination_language:
    raise SystemExit("You need different source and destination language!")


driver = webdriver.Firefox()




driver.get(f"https://translate.google.com/#{args.source_language}/{args.destination_language}/")


TranslateElement = "//input[@id='gt-submit']"

InputElement = "//textarea[@id='source']"

OutPutElement = "//div[@id='gt-res-dir-ctr']"


TranslateButton = WebDriverWait(driver, 100).until(lambda driver: driver.find_element_by_xpath(TranslateElement))

InputBox = WebDriverWait(driver, 10).until(lambda driver: 
driver.find_element_by_xpath(InputElement))


InputBox.send_keys(args.tobetranslated)


TranslateButton.click()


sleep(5)


ResultElement = WebDriverWait(driver, 10).until(lambda driver: driver.find_element_by_xpath(OutPutElement))


print(f"#### Result <{args.source_language}> -> <{args.destination_language}>")

print(ResultElement.text)

print("####")


driver.quit()

我希望上面的代码输出如下:

//compilation time

python Translator.py --source-language en --destination-language fr -tobetr
anslated "Hello"

//Output

bonjour

0 个答案:

没有答案