박정웅
박정웅

Reputation: 11

There is an element, why am I getting an NoSuchElementException?

problem:

Given the following code:

from selenium import webdriver

browser = webdriver.Chrome() 
browser.get('https://navercomp.wisereport.co.kr/v2/company/c1010001.aspx?cmp_cd=004000')

# move to my goal
browser.find_element_by_link_text("재무분석").click()
browser.find_element_by_link_text("재무상태표").click()

# extract the data 
elem = browser.find_element_by_xpath("//*[@id='faRVArcVR1a2']/table[2]/tbody/tr[2]/td[6]")
print(elem.text)

I write this code to extract finance data.

At first, I just move to page which have wanting data. And I copy the XPATH by Chrome Browser function.

But although there is 'text', I get faced NoSuchElementException. Why this problem happen?

try to fix:

At first, I thought that 'is this happen because of the delay'? Although there is almost no delay in my computer, I just try to fix it.

I add some import code and change 'elem' part:

from selenium import webdriver

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC


browser = webdriver.Chrome() 
browser.get('https://navercomp.wisereport.co.kr/v2/company/c1010001.aspx?cmp_cd=004000')

# move to my goal
browser.find_element_by_link_text("재무분석").click()
browser.find_element_by_link_text("재무상태표").click()

# extract the data 
elem = WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.XPATH, '//*[@id="faRVArcVR1a2"]/table[2]/tbody/tr[2]/td[6]')))
print(elem.text) 

but as a result, only TimeoutException happens.. I don't know why these problem happens. help pls! thank u..

Upvotes: 1

Views: 47

Answers (1)

user11718531
user11718531

Reputation:

from selenium import webdriver

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time

browser = webdriver.Chrome() 
browser.get('https://navercomp.wisereport.co.kr/v2/company/c1010001.aspx?cmp_cd=004000')

# move to my goal
browser.find_element_by_link_text("재무분석").click()
browser.find_element_by_link_text("재무상태표").click()

elementXpath = '//table[@summary="IFRS연결 연간 재무 정보를 제공합니다."][2]/tbody/tr[2]/td[6]'

# extract the data 
WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.XPATH, elementXpath)))
# Wait for the table to load
time.sleep(1)
elem = browser.find_element(by=By.XPATH, value=elementXpath)
print(elem.text)

There were several problems:

  • the ID of the div which wraps the table ("faRVArcVR1a2") changes every time you load the page, that's why this is not a proper way of finding the element. I changed that so that it is found by the summary of the table.
  • WebDriverWait doesn't return the element, that's why you have to get the element with find_element after you know it is present.
  • Even after you waited for the table to appear, you have to wait an additional second so that all cells of the table load. Otherwise you would get an empty string.

Upvotes: 1

Related Questions