为了账号安全,请及时绑定邮箱和手机立即绑定

网页抓取期间的 NoSuchElementException 和 SyntaxError

网页抓取期间的 NoSuchElementException 和 SyntaxError

叮当猫咪 2023-07-11 16:26:22
我在应用此建议来修复以下错误时遇到一些困难:NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"./ancestor-or-self::form"}当我使用以下代码时得到:from selenium import webdriverquery = ' 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')    breakexcept 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 回答

?
饮歌长啸

TA贡献1951条经验 获得超3个赞

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

输出

//img4.sycdn.imooc.com/64ad126f0001da7f14510601.jpg


查看完整回答
反对 回复 2023-07-11
?
狐的传说

TA贡献1804条经验 获得超3个赞

<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 


查看完整回答
反对 回复 2023-07-11
  • 2 回答
  • 0 关注
  • 82 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信