1

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:\Users\Admin\Downloads\chromedriver_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.

1
  • Since this is a top search engine hit for site:stackoverflow.com get_attribute Selenium, getAttribute() in Java, get_attribute() in Python, GetAttribute() in C#, attribute() in Ruby, getAttribute() in JavaScript, and getAttribute() in Kotlin... Commented Nov 28, 2022 at 0:58

1 Answer 1

1

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:\webdrivers\chromedriver.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
Sign up to request clarification or add additional context in comments.

11 Comments

it gives an error ` parent_of_element_to_search=element_to_search.find_element(By.XPATH("./..")) TypeError: 'str' object is not callable`. Does it mean I need to wrap element_to_search inside str() method?
Please see my updated answer and let me see what am I missing
As I see tr do not have class attribute. Also, you can locate the parent based on the child node, not by getting up with /..
Moment. So, is it working for you now and the problem is resolved or you getting an error?
Your code works well! Just the final result does not give me what I want. My main destination is to get in same <tr line to the 6th <td
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.