网络抓取期间出现NoSuchElementException和SyntaxError

时间:2020-09-01 22:19:16

标签: python selenium web-scraping selenium-chromedriver

在应用此建议来解决以下错误时,我遇到了一些困难:

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 - Unable to locate element,我应该使用类似的东西

WebDriverWait wait = new WebDriverWait(driver, 10);

wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt("source"));

但是我收到了SyntaxError(由于WebDriverWait wait =导致)。

我也尝试遵循以下答案:

NoSuchElementException (SyntaxError: too many statically nested blocks)

Selenium Webdriver - 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(

2 个答案:

答案 0 :(得分: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>

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 

答案 1 :(得分:0)

除了以下行:search.submit(),您已经完成了代码共享中所有的工作。调用submit()的Web元素方法和您定义的元素搜索时,它不是形式而是Textarea,因此是NoSuchElementException。因为Submit方法仅适用于form类型的元素。如果删除此行,您的代码将正常工作。

从硒导入网络驱动程序

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)

输出 Browser View of Output

注意:

要了解如何在Selenium python中使用不同的等待机制,请阅读以下内容:

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