亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

網頁抓取期間的 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)

輸出

http://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 關注
  • 174 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號