0

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(
  • 1
    `new` is not a python keyword ! – Cyril Jouve Sep 01 '20 at 22:27
  • Thanks. But I have got this code from a previous answer accepted. probably it was for a different programming language. –  Sep 01 '20 at 22:29
  • 1
    The error is about `"selector":"./ancestor-or-self::form"` but yet your code doesn't ever select this element – OneCricketeer Sep 01 '20 at 22:32
  • Not really clear why you need selenium at all... https://pypi.org/project/googletrans/ – OneCricketeer Sep 01 '20 at 22:34
  • It was asked me to use selenium for scraping it (I know about googletrans but unfortunately I cannot use it) –  Sep 01 '20 at 22:38
  • You're not really scraping here, though. You're using the web UI to make the same API request that that library would do. – OneCricketeer Sep 01 '20 at 22:40
  • My question is different: it is on how to fix this issue. I am doing web-scraping but the part of code related to that part is not relevant for fixing this issue. I think I followed all the guidelines and I do not understand why to down vote the question –  Sep 01 '20 at 22:45
  • You could try `textarea#source` or a function other than `find_element_by_css_selector` since the error is related to XPath parsing, but the issue lies within Selenium, I'm guessing, not your code – OneCricketeer Sep 01 '20 at 22:47
  • Thanks OneCricketeer. Unfortunately textarea#source does not work. I think there is some part related to xpath which is missing –  Sep 01 '20 at 22:49
  • The wait is in java language instead of python – Arundeep Chohan Sep 02 '20 at 01:42

2 Answers2

0
<textarea id="source" class="orig tlid-source-text-input goog-textarea" rows="1" spellcheck="false" autocapitalize="off" autocomplete="off" autocorrect="off" style="overflow: auto hidden; box-sizing: border-box; height: 70px; padding-bottom: 18px;"></textarea>

Xpaths can be either:

//*[@id='source']

/html/body/div[2]/div[2]/div[1]/div[2]/div[1]/div[1]/div[1]/div[2]/div/div/div[1]/textarea

Basically wait for an element and send the query and hit submit.

search = WebDriverWait(driver, 10).until( 
        EC.presence_of_element_located((By.XPATH, //*[@id='source'])) 
search.send_keys(query)
search.submit()

Also add these

from selenium.webdriver.common.by import By 
from selenium.webdriver.support.ui import WebDriverWait 
from selenium.webdriver.support import expected_conditions as EC 
Arundeep Chohan
  • 9,779
  • 5
  • 15
  • 32
0

You have done everything wright in your code shared at top except this line: search.submit(). As you are calling submit() method of web element and element search defined by you is no form rather its an Textarea,hence NoSuchElementException . Because submit method is applicable for only form type of elements. If you remove this line your code will work just fine.

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)

Output Browser View of Output

Note :

To know how to use different wait mechanisms in selenium python, below could be a good read:

https://selenium-python.readthedocs.io/waits.html

rahul rai
  • 2,260
  • 1
  • 7
  • 17