Skip to content Skip to sidebar Skip to footer

NoSuchElementException And SyntaxError During Web-scraping

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',

Solution 1:

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


Solution 2:

<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 

Post a Comment for "NoSuchElementException And SyntaxError During Web-scraping"