0

I need to click some rows in a table, the table is like this: table rows

the trs with bgcolor is those I've already reviewed. I need to skip them. I can't see the picture I uploaded, so I type some to acknowledge it.

<tr id ="1">...</tr>
<tr id ="2" bgcolor='B3D9D9'>...</tr>
<tr id ="3" bgcolor='B3D9D9'>...</tr>
<tr id ="4" >...</tr>
......

and the difference between tr1 and tr2, is the "bgcolor='B3D9D9'", and when I use web element inspector the check the css: below content is correspondingly more here in CSS style:

tr[Attributes Style] {
background-color: rgb(179, 217, 217);

}

here is my code:

trs = driver.find_elements_by_xpath('/ html / body / table / tbody / tr / td / div / div[3] / table / tbody / tr')
total = len(trs)
print(total)  # I got '21' here 
order = 1
selected = 0
for number in range(1,total):
     current_tr = driver.find_element_by_xpath('/ html / body / table / tbody / tr / td / div / div[3] / table / tbody / tr[%d]' % order)
     print(type(current_tr)) # class: webelement
     bgc = current_tr.get_attribute("bgcolor") 
     # print(bgc)  #None
     bgc2 = current_tr.get_attribute("background-color")
     # print(bgc2)  #None

     if current_tr.get_attribute("bgcolor") is not None: # == "#B3D9D9" 
        or current_tr.get_attribute("background-color") == "rgb(179, 217, 217)":
        print('pass')  #this is never been processed. so the if condition is never true. because it is None
        time.sleep(1)
        order = order + 1 
     else:
        driver.find_element_by_xpath('// *[ @ id = "%d"] / td[6] / a' % order).click() # current status is every row is clicked.
        selected = selected + 1
        time.sleep(3)
        

don't mind the format, I garantee the format is ok. And I finally get every tr clicked. what I want is to skip the trs with bgcolor property. I haven't get a "pass", that means get.attribute('bgcolor) is a none. But when I follow below Prophet's advice to add additional 'if', I still can not get pass. Which means is trully a None. and i've done print debugging, yes ,it's now. so the expression: get_attribute("bgcolor") =="#B3D9D9" is not correct? Why?? it's so obvious.please kindly spot my fault...

7
  • "that means get.attribute('bgcolor) is a none" - why do you assume it's None? You need to post more of your code. Perhaps current_tr is not getting changed so you're always checking the same element. We cannot tell since you've posted too little code. Preferably, post a minimal reproducible example. Commented Jul 13, 2021 at 15:19
  • it's changing. Because I have 'else' to handle click actions. When I iterate the trs, click action is ongoing. Only problem is what I said, I can not skip the trs with bgcolor. Commented Jul 14, 2021 at 1:02
  • You haven't shown where current_tr is getting set to any value much less changed. Commented Jul 14, 2021 at 14:15
  • Ok, I've attached what you want, I hope you can really help me. Commented Jul 15, 2021 at 1:20
  • 1
    current_tr = driver.find_element_by_xpath('/ html / body / table / tbody / tr / td / div / div[3] / table / tbody / tr[%d]' % order) is set within your for number in range(1,total): loop. But order does not change within your loop so you're checking the same element each time through the loop. Commented Jul 15, 2021 at 1:26

2 Answers 2

1

The value of current_tr does not change within the loop! Your XPATH (' ... table / tbody / tr[%d]' % order) uses the variable order whose value never changes from the initial value (1) so you're checking the same Web Element every time. Perhaps you meant to use the variable number instead of order for your XPATH.

Your code:

trs = driver.find_elements_by_xpath('/ html / body / table / tbody / tr / td / div / div[3] / table / tbody / tr')
total = len(trs)
print(total)  # I got '21' here 
order = 1
selected = 0
for number in range(1,total):
     current_tr = driver.find_element_by_xpath('/ html / body / table / tbody / tr / td / div / div[3] / table / tbody / tr[%d]' % order)
     # irrelevant code snipped
Sign up to request clarification or add additional context in comments.

2 Comments

Justin, you got it!
Good catch, Justin!
0

This occurs since there are elements in the table without bgcolor attribute.
So, when you trying to current_tr.get_attribute('bgcolor') on them it returns NoneType object.
Try this:

if current_tr.get_attribute('bgcolor') is not None:
    if current_tr.get_attribute('bgcolor') == '#B3D9D9' or current_tr.get_attribute('background-color') == 'rgb(179, 217, 217)':
        print('pass')

6 Comments

Hello, Prophet, thanks for your answer, I've tried your answer, but no effect. I have attached more code to elaborate my case: by the way , can you check the pictures. just for my inference, because even you can see the picture, it would be ok, because I've writen it down also.
Hello Prophet, I've checked your another answer for similiar question. get_attribute returns None. In my case, it's obviously attribute of the trs. And I've get the webelements.<class 'selenium.webdriver.remote.webelement.WebElement'> I was wondering why is None?
Probably the problem is in your code that is before the code you presented in the question. If current_tr is a tr web element as supposed applying .get_attribute('bgcolor') on it will return a string - the value of that attribute or None in case there is no such attribute for that element. .get_attribute() will never return a web element AFAIK
Yes, the proplem is returning None right now. of course it will never return a web element. My question is simple: why is returning None? the "bgcolor" attribute is obviously there.
instead of .get_attribute('bgcolor') try .value_of_css_property('background-color'). If still not working try value_of_css_property("color") or value_of_css_property("style"). Please let me know if that helped
|

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.