Search code examples
pythonxpathseleniumweb2py

selenium count elements of xpath


i have a webpage with a table containing many Download links i want to let selenium click on the last one :

table:

item1 Download
item2 Download
item3 Download

selenium must click on Download next item3

i used xpath to find all elments then get size of returned array or dict in this way

x = bot._driver.find_element_by_xpath("//a[contains(text(),'Download')]").size()

but i get always this error

TypeError: 'dict' object is not callable

i tried to use get_xpath_count methode but the methode doen't exist in selenium in python!

i thought about another solution but i don't know how to do it and it is as following

x = bot._driver.find_element_by_xpath("(//a[contains(text(),'Download')])[size()-1]")

or

x = bot._driver.find_element_by_xpath("(//a[contains(text(),'Download')])[last()]")

or something else


Solution

  • the best way to do this is to use JavaScript and find elements by class name

    self.bot._driver.execute_script("var e = document.getElementsByClassName('download'); e[e.length-1].click()")
    

    source: http://de.slideshare.net/adamchristian/javascript-testing-via-selenium