0

I'm using Selenium Python to count the number of attributes.

The html code is here:

<div id="leftw">
<a class="cat "  cid="1">BEVERAGE</a>
<a class="cat "  cid="8">APPS</a>
<a class="cat "  cid="2">SOUPS</a>
<a class="cat "  cid="9">SALADS</a>
<a class="cat "  cid="3">SANDWICHES</a>
<a class="cat "  cid="10">COMBOS</a>
<a class="cat "  cid="4">ENTREES</a>
<a class="cat "  cid="11">PIZZA</a>
<a class="cat "  cid="5">CALZONE</a>
<a class="cat "  cid="12">STROMBOLI</a>
<a class="cat "  cid="6">PASTRIES</a>
<a class="cat "  cid="13">DESSERTS</a>
<a class="cat "  cid="7">BREAD</a>
<a class="cat "  cid="14">SIDES</a>
<a class="cat "  cid="15">MEAT BY POUND</a>
<a class="cat "  cid="18">Kids Meal</a>
<a class="cat "  cid="19">MISC</a>
<a class="cat "  cid="0" ></a>
</div>

I want to count the a tag number whose cid value is not equal to 0.Because there is no value in a tag whose cid value equal 0.

if I run:

count_category = len(driver.find_elements_by_css_selector("#leftw .cat"))

I can only get the total number of all a tags. But I want to exclude the a tag whose cid value equals 0.

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
William
  • 3,724
  • 9
  • 43
  • 76

4 Answers4

1

can you try this?

count_category = len(driver.find_elements_by_xpath('//div[@id="lefw"]/a[not(@cid="0")]'))
joonghyup cha
  • 624
  • 3
  • 12
  • I really appreciate your answer,since I'm using css selector,so just copied the answer below yours,and it simply works,so I chose that one.But I really know your code will works too.Really appreciate! – William Dec 30 '19 at 18:11
  • Hi friend,can you help me with this question:https://stackoverflow.com/questions/59537965/how-to-handle-alert-or-pop-up-boxes-in-selenium-python – William Dec 30 '19 at 23:32
1

You have to add a pseudoselector :not to exclude a situation when an attribute cid equals to 0:

count_category = len(driver.find_elements_by_css_selector("#leftw .cat:not([cid='0'])"))
  • Hi friend,can you help me with this question:https://stackoverflow.com/questions/59537965/how-to-handle-alert-or-pop-up-boxes-in-selenium-python – William Dec 30 '19 at 23:32
1

To count the number of attributes whose cid attribute value is not equal to 0 using Selenium Python, you have to induce WebDriverWait for the visibility_of_all_elements_located() and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR:

    print(len(WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "div#leftw a.cat:not([cid='0'])")))))
    
  • Using XPATH:

    print(len(WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//div[@id='leftw']//a[@class='cat ' and not(@cid='0')]")))))
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • 1
    I really appreciate your answer,since I'm using css selector,so just copied the answer below yours,and it simply works,so I chose that one,but I do believe I will use your code in the future.Thanks again! – William Dec 30 '19 at 18:10
  • Hi friend,can you help me with this question:https://stackoverflow.com/questions/59537965/how-to-handle-alert-or-pop-up-boxes-in-selenium-python – William Dec 30 '19 at 23:32
0

Try following CSS selector which exclude cid='0'

count_category = len(driver.find_elements_by_css_selector("#leftw >a.cat:not([cid='0'])"))
KunduK
  • 32,888
  • 5
  • 17
  • 41
  • Hi friend,can you help me with this question:https://stackoverflow.com/questions/59537965/how-to-handle-alert-or-pop-up-boxes-in-selenium-python – William Dec 30 '19 at 23:32