Home > OS >  Getting an attribute from a table row with Selenium | Python
Getting an attribute from a table row with Selenium | Python

Time:02-04

I'm trying to find how can I see if this button (image) it's already clicked or not:

But I haven't been able to do it and I need to interact with this button because it allows me to edit the price of some items and protect them if I don't wan't, the problem is that I need to check if it's clicked or not before doing it.

I have tried many times to identify when it's clicked or not, but I don't see any change on button attributes so I don't know how to do it. I also tried checking on price item and I see one change when it's clicked:

When it's editable, there is an attribute named "contenteditable" and it's value equals true, but when it's not editable this attribute dissapear and changes for a attribute named "disabled" equals true:

This are the lines of code that I have been using to get this attribute values:

cod = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, '//*[@id="tbodyDatos"]/tr[1]/td[5]/div'))).get_attribute("disabled")

cod = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, '//*[@id="tbodyDatos"]/tr[1]/td[5]/div'))).get_attribute("attributes[1]")

cod = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, '//*[@id="tbodyDatos"]/tr[1]/td[5]/div'))).get_attribute("attributes[1].nodeValue")

I would appreciate it so much if anyone can help me.

¡Thanks!

CodePudding user response:

The expanded <i> along with it's decendants would have possibly given us some hint about some specific attribute to validate if the is checked or not.

checkbox


Solution

You can still probe the if the checkbox is checked or not with respect to the attribute of the adjascent <div> attribute as follows:

try:
    WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="tbodyDatos"]/tr[1]/td[5]/div'))).click()
    print("Checkbox unchecked")
except (TimeoutException, ElementNotInteractableException, WebDriverException):
    print("Checkbox checked")

Note : You have to add the following imports :

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException, ElementNotInteractableException, WebDriverException
  • Related