首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >页面更改时的NoSuchElementException

页面更改时的NoSuchElementException
EN

Stack Overflow用户
提问于 2022-10-13 19:32:45
回答 1查看 16关注 0票数 0

代码一直工作到curPage=12,并在curPage=13中停止并显示上面的错误。我尝试了By.XPATH &By.css-选择器,但它不起作用。此外,也没有定位元素的框架。

相同的XPath &CSS-选择器,但在页面更改后不工作。

如果有人有主意,请救救我。

代码语言:javascript
复制
url="https://home.kahis.go.kr/home/lkntscrinfo/selectLkntsOccrrncList.do"
time.sleep(3)
driver.get(url)
da = Alert(driver)

driver.implicitly_wait(3)
driver.find_element(By.XPATH, '//*[@id="dissCl"]/option[13]').click()

driver.implicitly_wait(3)
date=driver.find_element(By.XPATH, '//*[@id="occrFromDtId"]')
time.sleep(2)
date.send_keys("20170922")

driver.implicitly_wait(3)
driver.find_element(By.XPATH, '//*[@id="homeLkntscrinfoVO"]/table[3]/tbody/tr/td[3]/a').click()

curPage = 1
totPage = 184
cons=[]
idx=1

while curPage<=totPage:

  driver.implicitly_wait(3)
  if curPage==1:
    da.dismiss()
  html=driver.page_source
  soup=BeautifulSoup(html, 'html.parser')

  print('******* Current Page : ', curPage)

  trs=soup.findAll('tr', {'style' : 'background-color: #ffff80;'})

  for tr in trs:
    links=tr.findAll('td')
    for link in links:
      con=link.text
      cons.append(con)

  curPage+=1
  idx+=1
  if curPage%10==1:
    nextbar='//*[@id="homeLkntscrinfoVO"]/table[5]/tbody/tr[2]/td/table/tbody/tr/td[23]/a'
    driver.find_element(By.XPATH, nextbar).click()
    idx=idx-10
  else:
    driver.find_element(By.CSS_SELECTOR, '#homeLkntscrinfoVO > table:nth-child(7) > tbody > tr:nth-child(2) > td > table > tbody > tr > td:nth-child({}) > a'.format(1+idx*2)).click()

代码语言:javascript
复制
NoSuchElementException                    Traceback (most recent call last)
<ipython-input-157-50e7fb0938ad> in <module>
     61     idx=idx-10
     62   else:
---> 63     driver.find_element(By.CSS_SELECTOR, '#homeLkntscrinfoVO > table:nth-child(7) > tbody > tr:nth-child(2) > td > table > tbody > tr > td:nth-child({}) > a'.format(1+idx*2)).click()
     64 
     65 driver.close()

2 frames
/usr/local/lib/python3.7/dist-packages/selenium/webdriver/remote/errorhandler.py in check_response(self, response)
    241                 alert_text = value['alert'].get('text')
    242             raise exception_class(message, screen, stacktrace, alert_text)  # type: ignore[call-arg]  # mypy is not smart enough here
--> 243         raise exception_class(message, screen, stacktrace)

NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"#homeLkntscrinfoVO > table:nth-child(7) > tbody > tr:nth-child(2) > td > table > tbody > tr > td:nth-child(7) > a"}
  (Session info: headless chrome=105.0.5195.102)
EN

回答 1

Stack Overflow用户

发布于 2022-10-14 02:36:43

结果发现,每个xpath都有自己的元素“onclick”。因此,我更改了xpath,如下所示,它可以工作!

代码语言:javascript
复制
onClick='fn_page_link({}); return false;'.format(curPage)
driver.find_element(By.XPATH, '//a[contains(@onclick, onClick)]').click()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74060905

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档