网页抓取期间的 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"));

但我得到一个语法错误(由于WebDriverWait wait =)。


我也尝试遵循这些答案:


NoSuchElementException(语法错误:静态嵌套块太多)


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(


叮当猫咪
浏览 89回答 2
2回答

饮歌长啸

除了这一行之外,您已经完成了顶部共享代码中的所有操作: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)输出

狐的传说

<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(&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; EC.presence_of_element_located((By.XPATH, //*[@id='source']))&nbsp;search.send_keys(query)search.submit()还要添加这些from selenium.webdriver.common.by import By&nbsp;from selenium.webdriver.support.ui import WebDriverWait&nbsp;from selenium.webdriver.support import expected_conditions as EC&nbsp;
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python