NoSuchElementException e SyntaxError durante il web-scraping
Ho qualche difficoltà nell'applicazione di questo suggerimento per correggere il seguente errore:
NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"./ancestor-or-self::form"}
ottenuto quando utilizzo il codice seguente:
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()
Come spiegato qui: NoSuchElementException - Impossibile individuare l'elemento , dovrei usare qualcosa di simile
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt("source"));
Ma ottengo un SyntaxError (a causa di WebDriverWait wait =).
Ho provato anche a seguire queste risposte:
NoSuchElementException (SyntaxError: troppi blocchi annidati staticamente)
Selenium Webdriver - NoSuchElementExceptions
ma ricevo ancora errori:
try:
search = driver.find_element_by_css_selector('#source')
break
except NoSuchElementException:
time.sleep(1)
mi dà break outside the loop; mentre questo
try:
search = driver.find_element_by_css_selector('#source')
except NoSuchElementException:
pass
non cambia nulla (ancora mi dà l'errore: NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"./ancestor-or-self::form"})
Potresti aiutarmi a trovare un modo per correggere questi errori?
Aggiornamento: ho anche provato a utilizzare driver.implicitly_wait(60)e ho lo stesso NoSuchElementExpectionerrore.
Maggiori dettagli sull'errore:
---> 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(
Risposte
Hai fatto tutto wright nel codice condiviso in alto tranne che questa linea: search.submit(). Dato che stai chiamando il submit()metodo di ricerca degli elementi web e degli elementi definiti da te non è una forma piuttosto è un Textarea, quindi NoSuchElementException. Perché il metodo di invio è applicabile solo al formtipo di elementi. Se rimuovi questa riga, il tuo codice funzionerà perfettamente.
dal selenio 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)
Produzione
Nota :
Per sapere come utilizzare diversi meccanismi di attesa in selenio python, di seguito potrebbe essere una buona lettura:
https://selenium-python.readthedocs.io/waits.html
<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>
Gli Xpath possono essere:
//*[@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
Fondamentalmente attendi un elemento, invia la query e premi invia.
search = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH, //*[@id='source']))
search.send_keys(query)
search.submit()
Aggiungi anche questi
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC