网页抓取期间的 NoSuchElementException 和 SyntaxError [英] NoSuchElementException and SyntaxError during web-scraping

查看:25
本文介绍了网页抓取期间的 NoSuchElementException 和 SyntaxError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

NoSuchElementException:消息:没有这样的元素:无法定位元素:{方法":xpath",选择器":./ancestor-or-self::form"}

当我使用以下代码时得到:

from selenium import webdriverquery = '我想尝试翻译这段文字'chrome_options = webdriver.ChromeOptions('/chromedriver')驱动程序 = webdriver.Chrome()driver.get('https://translate.google.com/')search = driver.find_element_by_css_selector('#source')search.send_keys(查询)搜索.提交()

如此处所述:

注意:

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

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

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","selector":"./ancestor-or-self::form"}

got when I use the following code:

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()

As explained here: NoSuchElementException - Unable to locate element, I should use something like this

WebDriverWait wait = new WebDriverWait(driver, 10);

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

But I get a SyntaxError (due to WebDriverWait wait = ).

I have tried also to follow these answers:

NoSuchElementException (SyntaxError: too many statically nested blocks)

Selenium Webdriver - NoSuchElementExceptions

but I am still getting errors:

try:
    search = driver.find_element_by_css_selector('#source')

    break
except NoSuchElementException:
    time.sleep(1)

gives me break outside the loop; whereas this

try:
    search = driver.find_element_by_css_selector('#source')

except NoSuchElementException:         
    pass

does not change anything (still gives me the error: NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"./ancestor-or-self::form"})

Could you please help me to find a way to fix these errors?

Update: I also tried to use driver.implicitly_wait(60) and I have got the same NoSuchElementExpection error.

More detail on the error:

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

解决方案

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

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

这篇关于网页抓取期间的 NoSuchElementException 和 SyntaxError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆