How to find title and list where data-id is highest number

961 Views Asked by At

I want to click the element with highest data-id. I'm generating title like this:

char_set = string.ascii_uppercase
    tagTitle = "AI TAG " + ''.join(random.sample(char_set * 4, 4))
    driver.find_element_by_xpath("//*[@id='FolderName']").send_keys(tagTitle)

Currently I'm getting all elements of UI class:

driver.find_element_by_xpath("/html/body/div[2]/div[2]/div[1]/div[1]/div/div[2]/ul/li")

enter image description here

<ul class="investorGroup ul-groups">
   <li data-id="-1" class="">
      <a href="javascript:void(0)" onclick="$.InvestorContact.Group.LoadGroupInvestorsOnGroupClick(-1,null, 0)">Master</a>
   </li>
   <li title="AI TAG AOAI" data-id="371">
      <a href="javascript:void(0)" onclick="$.InvestorContact.Group.LoadGroupInvestorsOnGroupClick(371)">2451b 24 (<span class="contactCatCount">0</span>)</a>
      <a href="javascript:$.InvestorContact.Group.OpenAddGroupModal(371)" class="edit"><i class="fa fa-pencil" aria-hidden="true"></i></a>
   </li>
   <li title="AI TAG CANG" data-id="376" >
      <a href="javascript:void(0)" onclick="$.InvestorContact.Group.LoadGroupInvestorsOnGroupClick(376)">452352 (<span class="contactCatCount">0</span>)</a>
      <a href="javascript:$.InvestorContact.Group.OpenAddGroupModal(376)" class="edit"><i class="fa fa-pencil" aria-hidden="true"></i></a>
   </li>
</ul>

now tried and its showing the element :

$x('/html/body/div2/div2/div1/div1/div/div2/ul/li[contains(@title,"AI TAG FOVE")]')

enter image description here but does not click and gives error via python:

TagElement = driver.find_element_by_xpath('/html/body/div2/div2/div1/div1/div/div2/ul/li[contains(@title,"AI TAG FOVE")]') TagElement.click()

sorry if i skip something just a learner here.

selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element ... is not clickable at point (122, 388). Other element would receive the click: ... (Session info: chrome=80.0.3987.132)

2

There are 2 best solutions below

5
On

First you can get all the elements in a list and then you can click on the last element because that would be having the highest data-id and if you want to get the title which you are clicking then you can get it by using get_attribute() method.

You can do it like:

# Fetching the elements using xpath
title_list = driver.find_elements_by_xpath("//ul[@class='investorGroup ul-groups']//li[contains(@title,'AI TAG')]")  

# Getting the title of the last element
title_list[-1].get_attribute("title")   

# Clicking on the last element
title_list[-1].click()

Edited ans for picking text from a variable:

value = "AI TAG"

Now fetch the list by using:

title_list = driver.find_elements_by_xpath("//ul[@class='investorGroup ul-groups']//li[contains(@title,"+value+")]")  
7
On

First get all the <li> tag in a list present under particular <ul>. Iterate through the loop and get compare and store highest data-id attribute value element. Finally click on that highest value element.

Use below code :

list_of_li = driver.find_elements_by_css_selector('.investorGroup.ul-groups li')

container = 0
number = None
for element in list_of_li:
    number = element.get_attribute('data-id')

    if container < int(number):
        container = int(number)
        max_element = element

print(container)

max_element.click()