我正在使用selenium自动将一些文本从英语翻译成日语。此处有链接translate at deepl.com我在单击“翻译成日语”下拉列表中的“翻译成日语”按钮时遇到问题。为了更清晰,我在此附上照片click here for image。我使用了XPath,但有时它们可以工作,有时它们只是翻译成俄语而不是日语。这个按钮应该如何定位。我将非常感谢你的帮助
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
并在单击按钮之前传递一些预期条件:
driver.get('https://www.deepl.com/translator#en/de/...')
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "(//button[@class='lmt__language_select__active'])[2]"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "(//button[@dl-lang='JA'])[2]"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "(//textarea)[2]"))).click()
trad = [el.text for el in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//div[@class='lmt__textarea_base_style']/span[text()]")))]
print("".join(trad))
您可以添加以下导入:
并在单击按钮之前传递一些预期条件:
替代方法:使用API(see @Bertrand Martel's answer),或者在请求url中直接将“de”替换为“ja”
https://www.deepl.com/translator#en/de。。。到https://www.deepl.com/translator#en/ja
编辑:如果第二次单击失败,您可以使用
ActionChains
:相关问题 更多 >
编程相关推荐