0

I am trying to send a query to google translate using selenium. However I am finding difficult to find the class in order to pass arguments to the search box.

I have written the following:

chrome_options = webdriver.ChromeOptions()

driver = webdriver.Chrome('/chromedriver') 

   
driver.get('https://translate.google.com/')

query='Text to translate'
search = driver.find_element_by_name('q') # <- this is wrong!
search.send_keys(query)
search.submit()

Could you please point to the right class or argument to pass in my code? Thanks

1 Answers1

0

This works for me:

from selenium import webdriver

chrome_options = webdriver.ChromeOptions('/chromedriver')

driver = webdriver.Chrome()

driver.get('https://translate.google.com/')

query = 'Text to translate'
search = driver.find_element_by_css_selector('#source')
search.send_keys(query)

All I did was inspect the text area, and instead of find_element_by_name I used find_element_by_css_selector, so I copied the css selector of the text area. Also, you don't have to submit as it translates automatically.

F.M
  • 1,369
  • 1
  • 16
  • 31
  • @FM: I have got this error: `NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"./ancestor-or-self::form"}` Have you got any? –  Sep 01 '20 at 21:59
  • 1
    Try using `WebDriverWait` as stated here: https://stackoverflow.com/questions/39190910/nosuchelementexception-unable-to-locate-element – F.M Sep 01 '20 at 22:02
  • Thanks F.M. I have tried but I think I am doing something wrong in applying it. If I open a new question on it, would you be able to help me? –  Sep 01 '20 at 22:05
  • 1
    Sure, I guess so but do some research first before you ask – F.M Sep 01 '20 at 22:09
  • I have had a look at other answers but I am getting same or different errors, so I have opened a new question (if you would like to have a look at it): https://stackoverflow.com/questions/63696173/nosuchelementexception-and-syntaxerror-during-web-scraping –  Sep 01 '20 at 22:21
  • The problem seems to be related to ancestor-or-self which is missing –  Sep 01 '20 at 22:39