Webスクレイピング中のNoSuchElementExceptionとSyntaxError
次のエラーを修正するためにこの提案を適用するのにいくつかの問題があります。
NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"./ancestor-or-self::form"}
次のコードを使用すると取得します。
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()
ここで説明されているように:NoSuchElementException-要素を見つけることができません、私はこのようなものを使用する必要があります
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt("source"));
しかし、SyntaxErrorが発生します(によるWebDriverWait wait =)。
私はこれらの答えにも従おうとしました:
NoSuchElementException(SyntaxError:静的にネストされたブロックが多すぎます)
SeleniumWebdriver-NoSuchElementExceptions
しかし、まだエラーが発生しています:
try:
search = driver.find_element_by_css_selector('#source')
break
except NoSuchElementException:
time.sleep(1)
私に与えるbreak outside the loop; これに対して
try:
search = driver.find_element_by_css_selector('#source')
except NoSuchElementException:
pass
(まだ私にエラーを与える:何も変更されませんNoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"./ancestor-or-self::form"})
これらのエラーを修正する方法を見つけるのを手伝っていただけませんか。
更新:私も使用しようとしましたdriver.implicitly_wait(60)が、同じNoSuchElementExpectionエラーが発生しました。
エラーの詳細:
---> 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(
回答
次の行を除いて、上部で共有されているコードですべての作業を完了しましたsearch.submit()。あなたがsubmit()Web要素のメソッドを呼び出しているので、あなたが定義した要素検索は形式ではなくTextarea、したがってNoSuchElementExceptionです。送信方法はform要素のタイプにのみ適用できるためです。この行を削除すると、コードは問題なく機能します。
セレンインポートWebドライバーから
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)
出力
注意 :
セレンPythonでさまざまな待機メカニズムを使用する方法を知るには、以下をお読みください。
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>
Xpathは次のいずれかになります。
//*[@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
基本的に、要素を待ってクエリを送信し、送信を押します。
search = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH, //*[@id='source']))
search.send_keys(query)
search.submit()
これらも追加します
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC