I am having some difficulties in applying this suggestion to fix the following error:
NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"./ancestor-or-self::form"}
got when I use the following code:
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()
As explained here: NoSuchElementException - Unable to locate element, I should use something like this
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt("source"));
But I get a SyntaxError (due to WebDriverWait wait =
).
I have tried also to follow these answers:
NoSuchElementException (SyntaxError: too many statically nested blocks)
Selenium Webdriver - NoSuchElementExceptions
but I am still getting errors:
try:
search = driver.find_element_by_css_selector('#source')
break
except NoSuchElementException:
time.sleep(1)
gives me break outside the loop
; whereas this
try:
search = driver.find_element_by_css_selector('#source')
except NoSuchElementException:
pass
does not change anything (still gives me the error: NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"./ancestor-or-self::form"}
)
Could you please help me to find a way to fix these errors?
Update: I also tried to use driver.implicitly_wait(60)
and I have got the same NoSuchElementExpection
error.
More detail on the error:
---> 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(