我在应用此建议来修复以下错误时遇到一些困难:
NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"./ancestor-or-self::form"}
当我使用以下代码时得到:
from selenium import webdriver
query = ' I want to try to translate this text'
chrome_options = webdriver.ChromeOptions('/chromedriver')
driver = webdriver.Chrome()
driver.get('https://translate.google.com/')
search = driver.find_element_by_css_selector('#source')
search.send_keys(query)
search.submit()
正如这里所解释的:NoSuchElementException - 无法定位元素,我应该使用这样的东西
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt("source"));
但我得到一个语法错误(由于WebDriverWait wait =)。
我也尝试遵循这些答案:
NoSuchElementException(语法错误:静态嵌套块太多)
Selenium Webdriver - NoSuchElementExceptions
但我仍然收到错误:
try:
search = driver.find_element_by_css_selector('#source')
break
except NoSuchElementException:
time.sleep(1)
给我break outside the loop;而这
try:
search = driver.find_element_by_css_selector('#source')
except NoSuchElementException:
pass
没有改变任何东西(仍然给我错误NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"./ancestor-or-self::form"}:)
您能帮我找到解决这些错误的方法吗?
更新:我也尝试使用driver.implicitly_wait(60),但遇到了同样的NoSuchElementExpection错误。
有关错误的更多详细信息:
---> 23 search.submit()
24
25
~/anaconda3/lib/python3.7/site-packages/selenium/webdriver/remote/webelement.py in submit(self)
83 """Submits a form."""
84 if self._w3c:
---> 85 form = self.find_element(By.XPATH, "./ancestor-or-self::form")
86 self._parent.execute_script(
饮歌长啸
狐的传说
相关分类