'get_attribute' method in Python Selenium

Question:

The get_attribute() method in Python Selenium gives an error:

Did you mean ‘getattribute‘.

Why do I need that?

I am trying to get the parent elements class attribute to know if I got to the right DOM place.

Enter image description here

import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from datetime import datetime
#import pandas as pd

driver = webdriver.Chrome(r"C:UsersAdminDownloadschromedriver_win32 (1)chromedriver.exe")
driver.get("https://www.nba.com/schedule?pd=false&region=1")
driver.implicitly_wait(5)
element_to_click = driver.find_element(By.ID, "onetrust-accept-btn-handler") #.click()
element_to_click.click()
element_to_save = driver.find_element(By.XPATH, "//div/div/div/div/h4")

#Element_to_save.to_excel("3row, 3column)")
f = open('result_file00.txt', 'r+')
f.write(element_to_save.text)
f.write("n")
f.write(str(datetime.today()))
myList = []
myList.append(1)

elements_to_save = driver.find_elements(By.XPATH, "//*[@data-id='nba:schedule:main:team:link']")
for element in elements_to_save:
    f.write(" ")
    f.write(element.text)
    myList.append(element.text)
    f.write(" n ")
    f.write(str(datetime.today()))

f.close()
f = open('result_file00.txt', 'r+')
print(f.read())
f.close()
print(myList)
print(type(myList))

time.sleep(1)
driver.get("https://www.nba.com/stats/teams/traditional")
element_to_search = driver.find_element(By.LINK_TEXT, myList[1])
parentof_element_to_search = element_to_search.parent
print(parentof_element_to_search.get_attribute("class")) # Error-giving line

driver.quit()

I tried parentof_element_to_search = element_to_search.find_element(By.XPATH("..")) to get the parent element. Then trying to get the parent class of that element with parentof_element_to_search.get_attribute("class") resulted in the same error.

My desired code snippet from this result is getting the value of the 6-th <td> element in that <tr>.
find_element(By.XPATH("//td[6]"), the green line in the photo.

In brief, I get the team name’s <td> line, then coming back to the same <tr> tag and getting 6 step for the <td> value.

Asked By: xlmaster

||

Answers:

You are not actually getting the parent element with parentof_element_to_search=element_to_search.find_element(By.XPATH(".."))

Try this instead:

parent_of_element_to_search = element_to_search.find_element(By.XPATH("./.."))

Now you will be able to apply get_attribute() on it like the following:

parent_of_element_to_search = element_to_search.find_element(By.XPATH("./.."))
parent_element_classes = parent_of_element_to_search.get_attribute("class")

This is what I tried:

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

options = Options()
options.add_argument("start-maximized")

webdriver_service = Service('C:webdriverschromedriver.exe')
driver = webdriver.Chrome(service=webdriver_service, options=options)
wait = WebDriverWait(driver, 10)
url = 'https://www.nba.com/schedule?pd=false&region=1'
driver.get(url)

myList = []
myList.append(1)
wait.until(EC.element_to_be_clickable((By.ID, "onetrust-accept-btn-handler"))).click()
elements = wait.until(EC.visibility_of_all_elements_located((By.XPATH, "//*[@data-id='nba:schedule:main:team:link']")))
for element in elements:
    myList.append(element.text)


driver.get("https://www.nba.com/stats/teams/traditional")
element_to_search = wait.until(EC.visibility_of_element_located((By.LINK_TEXT, myList[1])))
classes = element_to_search.find_element(By.XPATH, "./..").get_attribute("class")
print(classes)

And this is the output of my program run:

Crom_text__NpR1_ Crom_primary__EajZu StatsTeamsTraditionalTable_primary__U2_3n Crom_stickySecondColumn__29Dwf
Answered By: Prophet