skip to Main Content

I’m trying to get a table from a dynamic webpage using selenium but it’s giving me issues. This is the Python code:

from selenium import webdriver

url = 'https://draft.shgn.com/nfc/public/dp/788/grid' 
driver = webdriver.Chrome('C:chromedriver_win32chromedriver.exe')

global_dynamicUrl = "https://draft.shgn.com/nfc/public/dp/788/grid"
driver.get(global_dynamicUrl)
table_area = driver.find_element("xpath", '//*[@id="result"]/table')
table_entries = table_area.find_elements_by_tag_name("tr")
print(len(table_entries))
driver.close()

But this produces a "NoSuchElementException" Error.

What am I doing wrong?

Thanks in advance.

3

Answers


  1. Atleast i don’t see the above-mentioned locator in website, you can try something similar like below.
    enter image description here

    Login or Signup to reply.
  2. From the URL you have provided, I don’t see any web element in the DOM matching your XPath expression – //*[@id="result"]/table. So the reason for your NoSuchElementException is the incorrect XPath with which you are trying to get the desired element.

    Below code locates table’s column headers: (Returns one tr)

    driver.find_element("xpath", '//table//thead//tr')
    

    Below code locates all other tr nodes of the table: (Returns 30 tr)

    table_entries = driver.find_elements(By.XPATH, "//table//tbody//tr[@class='ng-scope']")
    print(len(table_entries))
    
    Login or Signup to reply.
  3. There is no element with an ID of "result" on the page so that’s why your locator isn’t working. There is only one TABLE tag on the page so you can simplify your code down to

    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.wait import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    
    global_dynamicUrl = "https://draft.shgn.com/nfc/public/dp/788/grid"
    driver.get(global_dynamicUrl)
    wait = new WebDriverWait(driver, 10)
    table_entries = wait.until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "table tr"))
    print(len(table_entries))
    driver.close()
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search