Issue
I need to click some rows in a table, the table is like this:
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...
Solution
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
Answered By - Justin Ezequiel
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.